颐和api

DateTimeHelper.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. namespace MadRunFabric.Common
  6. {
  7. public static class DateTimeHelper
  8. {
  9. /// <summary>
  10. /// 时间戳生成规则
  11. /// </summary>
  12. /// <returns></returns>
  13. public static Int64 GetTimeStamp()
  14. {
  15. var ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
  16. return (Int64)ts.TotalSeconds;
  17. }
  18. public static string GetTime()
  19. {
  20. return DateTime.Now.ToString("HH:mm");
  21. }
  22. public static string GetDate()
  23. {
  24. return DateTime.Now.ToString("yyyy-MM-dd");
  25. }
  26. public static string GetChineseDate()
  27. {
  28. return DateTime.Now.ToString("yyyy年MM月dd日");
  29. }
  30. public static string GetDateTime()
  31. {
  32. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  33. }
  34. public static int GetDayOfWeek()
  35. {
  36. return (int)DateTime.Now.DayOfWeek;
  37. }
  38. public static int GetDayOfYear()
  39. {
  40. return DateTime.Now.DayOfYear;
  41. }
  42. public static string GetDateTimeStringFromCulture(DateTime time, CultureInfo cul) =>
  43. time.ToString(cul.DateTimeFormat);
  44. /// <summary>
  45. /// 获取系统时区(不返回null)
  46. /// </summary>
  47. /// <returns></returns>
  48. public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZone()
  49. {
  50. var collection = TimeZoneInfo.GetSystemTimeZones();
  51. if (collection == null)
  52. {
  53. collection = new ReadOnlyCollection<TimeZoneInfo>(new List<TimeZoneInfo>() { });
  54. }
  55. return collection;
  56. }
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <returns></returns>
  61. public static string GetWeek(DateTime? time = null)
  62. {
  63. string week = string.Empty;
  64. switch ((time ?? DateTime.Now).DayOfWeek)
  65. {
  66. case DayOfWeek.Sunday: week = "星期日"; break;
  67. case DayOfWeek.Monday: week = "星期一"; break;
  68. case DayOfWeek.Tuesday: week = "星期二"; break;
  69. case DayOfWeek.Wednesday: week = "星期三"; break;
  70. case DayOfWeek.Thursday: week = "星期四"; break;
  71. case DayOfWeek.Friday: week = "星期五"; break;
  72. case DayOfWeek.Saturday: week = "星期六"; break;
  73. default: week = "错误的星期"; break;
  74. }
  75. return week;
  76. }
  77. /// <summary>
  78. /// 获取友好的时间格式
  79. /// </summary>
  80. /// <param name="date"></param>
  81. /// <returns></returns>
  82. public static string GetFriendlyDateTime(DateTime date)
  83. {
  84. try
  85. {
  86. if (date == null) { return string.Empty; }
  87. var now = DateTime.Now;
  88. var cursor = now;
  89. if (date > cursor)
  90. {
  91. var span = date - now;
  92. if (span.TotalDays < 1)
  93. {
  94. return $"明天{date.Hour}点{date.Minute}分";
  95. }
  96. if (span.TotalDays < 2)
  97. {
  98. return $"后天{date.Hour}点{date.Minute}分";
  99. }
  100. return date.ToString();
  101. }
  102. else
  103. {
  104. //计算时间差
  105. var span = cursor - date;
  106. //如果是十分钟之内就显示几分钟之前
  107. if (span.TotalMinutes >= 0 && span.TotalMinutes < 10)
  108. {
  109. if (((int)span.TotalMinutes) == 0) { return "刚刚"; }
  110. return $"{(int)Math.Floor(span.TotalMinutes)}分钟前";
  111. }
  112. //今天
  113. cursor = now.Date;
  114. if (date >= cursor && date < cursor.AddDays(1))
  115. {
  116. return $"今天 {date.Hour}:{date.Minute}";
  117. }
  118. //昨天
  119. cursor = now.Date.AddDays(-1);
  120. if (date >= cursor && date < cursor.AddDays(1))
  121. {
  122. return $"昨天 {date.Hour}:{date.Minute}";
  123. }
  124. //前天
  125. cursor = now.Date.AddDays(-2);
  126. if (date >= cursor && date < cursor.AddDays(1))
  127. {
  128. return $"前天 {date.Hour}:{date.Minute}";
  129. }
  130. //不是最近三天 看是不是今年
  131. if (date.Year != now.Year)
  132. {
  133. return $"{date.Year}年{date.Month}月{date.Day}日 {date.Hour}:{date.Minute}";
  134. }
  135. else
  136. {
  137. return $"今年{date.Month}月{date.Day}日 {date.Hour}:{date.Minute}";
  138. }
  139. }
  140. }
  141. catch (Exception e)
  142. {
  143. return "时间转换错误";
  144. }
  145. }
  146. /// <summary>
  147. /// 获取友好的时间格式
  148. /// </summary>
  149. /// <param name="date"></param>
  150. /// <returns></returns>
  151. public static string GetSimpleFriendlyDateTime(DateTime date)
  152. {
  153. try
  154. {
  155. if (date == null) { return string.Empty; }
  156. var now = DateTime.Now;
  157. if (date > now)
  158. {
  159. var span = date - now;
  160. if (span.TotalDays < 1)
  161. {
  162. return $"明天{date.Hour}点{date.Minute}分";
  163. }
  164. if (span.TotalDays < 2)
  165. {
  166. return $"后天{date.Hour}点{date.Minute}分";
  167. }
  168. return date.ToString();
  169. }
  170. else
  171. {
  172. //计算时间差
  173. var span = now - date;
  174. if (span.TotalMinutes < 1)
  175. {
  176. return "刚刚";
  177. }
  178. else if (span.TotalHours < 1)
  179. {
  180. return $"{(int)span.TotalMinutes}分钟前";
  181. }
  182. else if (span.TotalDays < 1)
  183. {
  184. return $"{(int)span.TotalHours}小时前";
  185. }
  186. else if (span.TotalDays < 31)
  187. {
  188. return $"{(int)span.TotalDays}天前";
  189. }
  190. else if (span.TotalDays < 365)
  191. {
  192. return $"{(int)(span.TotalDays / 31)}月前";
  193. }
  194. else
  195. {
  196. return $"{(int)(span.TotalDays / 365)}年前";
  197. }
  198. }
  199. }
  200. catch (Exception e)
  201. {
  202. return "时间转换错误";
  203. }
  204. }
  205. }
  206. }