StringToDateConvert.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows.Data;
  9. namespace PDF_Master.DataConvert
  10. {
  11. /// <summary>
  12. /// 用于注释 书签等的时间转换
  13. /// </summary>
  14. public class StringToDateConvert : IValueConverter
  15. {
  16. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  17. {
  18. string date = "";
  19. if (string.IsNullOrEmpty((string)value) || !Regex.IsMatch(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  20. {
  21. date = System.DateTime.Now.ToString("HH:mm");
  22. }
  23. else if (Regex.IsMatch(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  24. {
  25. string dateStr = Regex.Match(value.ToString(), "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  26. #region to do
  27. if (string.Equals(dateStr.Substring(4, 4), DateTime.Now.ToString("MMdd")))
  28. {//同天
  29. date = dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2);
  30. }
  31. else if (string.Equals(dateStr.Substring(0, 4), DateTime.Now.ToString("yyyy")))
  32. {//同年
  33. date = dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" +
  34. dateStr.Substring(10, 2);
  35. }
  36. else
  37. {
  38. date = dateStr.Substring(0, 4) + "/" + dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" +
  39. dateStr.Substring(10, 2);
  40. }
  41. #endregion to do
  42. //date = dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + " " + dateStr.Substring(8, 2) + ":" + dateStr.Substring(10, 2);
  43. }
  44. return date;
  45. }
  46. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  47. {
  48. return null;
  49. }
  50. }
  51. //首页-最近列表时间格式
  52. public class RecentFileDateTimeToStringConvert : IValueConverter
  53. {
  54. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  55. {
  56. if (value == null || value is DateTime == false || (DateTime)value == null)
  57. return null;
  58. string tag = "";
  59. var last = (DateTime)value;
  60. var now = System.DateTime.Now;
  61. //时间间隔
  62. //TimeSpan ts = (now - last).Duration();
  63. //7天之内
  64. bool isSameWeek = false;
  65. //bool isSameWeek = !(ts.TotalDays >= 7 || (DateTime.Compare(now, last) > 0 ? (now.DayOfWeek < last.DayOfWeek) : (now.DayOfWeek > last.DayOfWeek)));
  66. isSameWeek = WeekSpan(now, last);
  67. //同年同月
  68. if (now.Year == last.Year)
  69. { //同日
  70. if (now.Month == last.Month)
  71. {
  72. if (now.Day == last.Day)
  73. {
  74. tag = "Today";
  75. }
  76. else if (isSameWeek)
  77. {
  78. tag = "week";
  79. }
  80. }
  81. }
  82. string result = "";
  83. var cultureInfo = CultureInfo.InvariantCulture;
  84. CultureInfo cultureInfoEnglish = new CultureInfo("en-US");
  85. switch (tag)
  86. {
  87. case "Today":
  88. result = "Today, " + last.Hour + ":" + last.Minute;
  89. break;
  90. case "week":
  91. string weekStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd"), cultureInfo).ToString("ddd", cultureInfoEnglish);
  92. result = weekStr + ", " + last.Hour + ":" + last.Minute;
  93. break;
  94. default:
  95. string monthStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd"), cultureInfo).ToString("MMM", cultureInfoEnglish);
  96. result = monthStr + " " + last.Day + ", " + last.Year;
  97. break;
  98. }
  99. return result;
  100. }
  101. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public bool WeekSpan(DateTime now, DateTime last)
  106. {
  107. int ts = DateTime.Compare(now, last) > 0 ? (now.Day - last.Day) : (last.Day - now.Day);
  108. int a = DateTime.Compare(now, last) > 0 ? (now.DayOfWeek - last.DayOfWeek) : (last.DayOfWeek - now.DayOfWeek);
  109. if (ts == a) { return true; }
  110. return false;
  111. }
  112. }
  113. }