using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Data; namespace PDF_Master.DataConvert { /// /// 用于注释 书签等的时间转换 /// public class StringToDateConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string date = ""; if (string.IsNullOrEmpty((string)value) || !Regex.IsMatch(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])")) { date = System.DateTime.Now.ToString("HH:mm"); } else if (Regex.IsMatch(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])")) { string dateStr = Regex.Match(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value; #region to do if (string.Equals(dateStr.Substring(4, 4), DateTime.Now.ToString("MMdd"))) {//同天 date = dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2); } else if (string.Equals(dateStr.Substring(0, 4), DateTime.Now.ToString("yyyy"))) {//同年 date = dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2); } else { date = dateStr.Substring(0, 4) + "/" + dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2); } #endregion to do //date = dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2); } return date; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } //首页-最近列表时间格式 public class RecentFileDateTimeToStringConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || value is DateTime == false || (DateTime)value == null) return null; string tag = ""; var last = (DateTime)value; var now = System.DateTime.Now; //时间间隔 //TimeSpan ts = (now - last).Duration(); //7天之内 bool isSameWeek = false; //bool isSameWeek = !(ts.TotalDays >= 7 || (DateTime.Compare(now, last) > 0 ? (now.DayOfWeek < last.DayOfWeek) : (now.DayOfWeek > last.DayOfWeek))); isSameWeek = WeekSpan(now, last); //同年同月 if (now.Year == last.Year) { //同日 if (now.Month == last.Month) { if (now.Day == last.Day) { tag = "Today"; } else if (isSameWeek) { tag = "week"; } } } string result = ""; var cultureInfo = CultureInfo.InvariantCulture; CultureInfo cultureInfoEnglish = new CultureInfo("en-US"); switch (tag) { case "Today": result = "Today, " + last.Hour + ":" + last.Minute; break; case "week": string weekStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd"), cultureInfo).ToString("ddd", cultureInfoEnglish); result = weekStr + ", " + last.Hour + ":" + last.Minute; break; default: string monthStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd"), cultureInfo).ToString("MMM", cultureInfoEnglish); result = monthStr + " " + last.Day + ", " + last.Year; break; } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public bool WeekSpan(DateTime now, DateTime last) { int ts = DateTime.Compare(now, last) > 0 ? (now.Day - last.Day) : (last.Day - now.Day); int a = DateTime.Compare(now, last) > 0 ? (now.DayOfWeek - last.DayOfWeek) : (last.DayOfWeek - now.DayOfWeek); if (ts == a) { return true; } return false; } } }