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_Office.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(); //同一周 bool isSameWeek = !(ts.TotalDays >= 7 || (DateTime.Compare(now, last) > 0 ? (now.DayOfWeek < last.DayOfWeek) : (now.DayOfWeek > last.DayOfWeek))); //同年同月 if (now.Year == last.Year) { //同日 if (now.Day == last.Day) { tag = "Today"; } { if (isSameWeek) tag = "week"; } } string result = ""; switch(tag) { case "Today": result = "Today, " + last.Hour + ":" + last.Minute; break; case "week": string weekStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd")).ToString("ddd"); result = weekStr + ", " + last.Hour + "," + last.Minute; break; default: string monthStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd")).ToString("MMM"); result = monthStr + " " + last.Day + ", " + last.Year; break; } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }