Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public static class DateExtensions
  2. {
  3. public static string ToText(this DateTime date)
  4. {
  5. var hour = (DateField)date.Hour;
  6. bool zy = false, zm = false, zd = false;
  7. if ((hour & DateField.Year) > 0)
  8. zy = true;
  9. if ((hour & DateField.Month) > 0)
  10. zm = true;
  11. if ((hour & DateField.Day) > 0)
  12. zd = true;
  13.  
  14. if (zd & zm)
  15. return $"год {date.Year} ";
  16.  
  17. if (zd)
  18. return $"год {date.Year} месяц {date.Month}";
  19.  
  20. if (zy)
  21. return $"год не указан";
  22.  
  23. return date.ToShortDateString();
  24. }
  25.  
  26. public static string ToText(this DateTime? date) => date.HasValue ? ToText(date.Value) : "";
  27.  
  28. public static DateTime Build(int year, int month, int day)
  29. {
  30. DateField mask = 0;
  31. if (year == 0)
  32. {
  33. year = 1;
  34. mask = mask | DateField.Year;
  35. }
  36.  
  37. if (month == 0)
  38. {
  39. month = 1;
  40. mask = mask | DateField.Month;
  41. }
  42.  
  43. if (day == 0)
  44. {
  45. day = 1;
  46. mask = mask | DateField.Day;
  47. }
  48.  
  49. return new DateTime(year, month, day, mask, 0, 0);
  50. }
  51.  
  52. [Flags]
  53. enum DateField
  54. {
  55. Year = 1,
  56. Month = 2,
  57. Day = 4
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement