StringToDateConvert.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_Office.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. TimeSpan ts = (now - last).Duration();
  62. //同一周
  63. bool isSameWeek = !(ts.TotalDays >= 7 || (DateTime.Compare(now, last) > 0 ? (now.DayOfWeek < last.DayOfWeek) : (now.DayOfWeek > last.DayOfWeek)));
  64. //同年同月
  65. if (now.Year == last.Year)
  66. {
  67. //同日
  68. if (now.Day == last.Day)
  69. {
  70. tag = "Today";
  71. }
  72. {
  73. if (isSameWeek)
  74. tag = "week";
  75. }
  76. }
  77. string result = "";
  78. switch(tag)
  79. {
  80. case "Today":
  81. result = "Today, " + last.Hour + ":" + last.Minute;
  82. break;
  83. case "week":
  84. string weekStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd")).ToString("ddd");
  85. result = weekStr + ", " + last.Hour + "," + last.Minute;
  86. break;
  87. default:
  88. string monthStr = DateTime.Parse(last.Date.ToString("yyyy-MM-dd")).ToString("MMM");
  89. result = monthStr + " " + last.Day + ", " + last.Year;
  90. break;
  91. }
  92. return result;
  93. }
  94. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. }
  99. }