StringToDateConvert.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }