Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

178 linhas
6.8 KiB

  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Globalization;
  5. namespace AbrBlazorTools
  6. {
  7. public static class DateTimeExtension
  8. {
  9. public static string ComputedDateTime(this DateTime InsertDateTime)
  10. {
  11. var persianCalender = new PersianCalendar();
  12. var year = persianCalender.GetYear(InsertDateTime);
  13. var month = persianCalender.GetMonth(InsertDateTime);
  14. var day = persianCalender.GetDayOfMonth(InsertDateTime);
  15. var hour = persianCalender.GetHour(InsertDateTime);
  16. var minute = persianCalender.GetMinute(InsertDateTime);
  17. var second = persianCalender.GetSecond(InsertDateTime);
  18. var computed = String.Format("{0}-{1}-{2}-{3}{4}{5}", year,
  19. month < 10 ? String.Format("0{0}", month) : month.ToString(),
  20. day < 10 ? String.Format("0{0}", day) : day.ToString(),
  21. hour < 10 ? string.Format("0{0}", hour) : hour.ToString(),
  22. minute < 10 ? string.Format("0{0}", minute) : minute.ToString(),
  23. second < 10 ? string.Format("0{0}", second) : second.ToString());
  24. return computed;
  25. }
  26. public static string GetPersianDate(this DateTime value, bool showDayName, bool showMonthName, bool showTime)
  27. {
  28. if (Services.Statics.Culture == Services.Statics.CultureType.En)
  29. return value.ToString();
  30. var persianCalender = new PersianCalendar();
  31. var year = persianCalender.GetYear(value);
  32. var month = persianCalender.GetMonth(value);
  33. var day = persianCalender.GetDayOfMonth(value);
  34. var s = string.Format(showMonthName ? "{2} {1} {0}" : "{0}/{1}/{2}", year, showMonthName ? month.GetMonthName() : month.ToString(""), day);
  35. if (showDayName)
  36. s = string.Format("{0} {1}", persianCalender.GetDayOfWeek(value).GetDayName(), s);
  37. if (showTime)
  38. s = string.Format("{0} {1}:{2}", s, value.Hour, value.Minute);
  39. return s;
  40. }
  41. public static string GetPersianDayName(this DateTime value)
  42. {
  43. var persianCalender = new PersianCalendar();
  44. var s = persianCalender.GetDayOfWeek(value).GetDayName();
  45. return s;
  46. }
  47. public static string GetPersianMonthName(this DateTime value)
  48. {
  49. var persianCalender = new PersianCalendar();
  50. var s = persianCalender.GetMonth(value).GetMonthName();
  51. return s;
  52. }
  53. public static int GetPersianDayInt(this DateTime value)
  54. {
  55. var persianCalender = new PersianCalendar();
  56. var s = persianCalender.GetDayOfMonth(value);
  57. return s;
  58. }
  59. public static string GetPersianDate(this DateTime value)
  60. {
  61. return value.GetPersianDate(true, true, true);
  62. }
  63. public static string GetPersianDate(this DateTime? value)
  64. {
  65. return value.HasValue ? value.Value.GetPersianDate(true, true, true) : "";
  66. }
  67. public static string GetPersianDate(this DateTime? value, bool showDayName)
  68. {
  69. return value.HasValue ? value.Value.GetPersianDate(showDayName, false, false) : "";
  70. }
  71. public static string GetPersianDate(this DateTime value, bool showDayName)
  72. {
  73. return value.GetPersianDate(showDayName, false, false);
  74. }
  75. public static string GetPersianDate(this DateTime value, bool showDayName, bool showTime)
  76. {
  77. return value.GetPersianDate(showDayName, false, showTime);
  78. }
  79. public static string GetPersianDateAsText(this DateTime value, bool showDayName, bool showTime)
  80. {
  81. value=value.ToUniversalTime().ToLocalTime();
  82. var minutes = (int)DateTime.Now.Subtract(value).TotalMinutes;
  83. var seconds = (int)DateTime.Now.Subtract(value).TotalSeconds;
  84. if (minutes < 1)
  85. if (Services.Statics.Culture == Services.Statics.CultureType.Fa)
  86. return $"{seconds} ثانیه قبل";
  87. else
  88. return $"{seconds} Second(s) ago";
  89. else if (minutes < 60)
  90. if (Services.Statics.Culture == Services.Statics.CultureType.Fa)
  91. return $"{minutes} دقیقه قبل";
  92. else
  93. return $"{minutes} Minute(s) ago";
  94. return value.GetPersianDate(showDayName, false, showTime);
  95. }
  96. private static string GetMonthName(this int month)
  97. {
  98. switch (month)
  99. {
  100. case 1:
  101. return "فروردین";
  102. case 2:
  103. return "اردیبهشت";
  104. case 3:
  105. return "خرداد";
  106. case 4:
  107. return "تیر";
  108. case 5:
  109. return "امرداد";
  110. case 6:
  111. return "شهریور";
  112. case 7:
  113. return "مهر";
  114. case 8:
  115. return "آبان";
  116. case 9:
  117. return "آذر";
  118. case 10:
  119. return "دی";
  120. case 11:
  121. return "بهمن";
  122. case 12:
  123. return "اسفند";
  124. default: return "";
  125. }
  126. }
  127. public static string GetDayName(this DayOfWeek day)
  128. {
  129. switch (day)
  130. {
  131. case DayOfWeek.Friday:
  132. return "جمعه";
  133. case DayOfWeek.Monday:
  134. return "دوشنبه";
  135. case DayOfWeek.Saturday:
  136. return "شنبه";
  137. case DayOfWeek.Sunday:
  138. return "یک شنبه";
  139. case DayOfWeek.Thursday:
  140. return "پنج شنبه";
  141. case DayOfWeek.Tuesday:
  142. return "سه شنبه";
  143. case DayOfWeek.Wednesday:
  144. return "چهار شنبه";
  145. default:
  146. return "";
  147. }
  148. }
  149. }
  150. public enum PersianDays
  151. {
  152. [Display(Name = "شنبه")]
  153. Saturday = (int)(DayOfWeek.Saturday),
  154. [Display(Name = "یکشنبه")]
  155. Sunday = (int)(DayOfWeek.Sunday),
  156. [Display(Name = "دوشنبه")]
  157. Monday = (int)(DayOfWeek.Monday),
  158. [Display(Name = "سه شنبه")]
  159. Tuesday = (int)(DayOfWeek.Tuesday),
  160. [Display(Name = "چهار شنبه")]
  161. Wednesday = (int)(DayOfWeek.Wednesday),
  162. [Display(Name = "پنج شنبه")]
  163. Thursday = (int)(DayOfWeek.Thursday),
  164. [Display(Name = "چمعه")]
  165. Friday = (int)(DayOfWeek.Friday),
  166. }
  167. }