DateTimeUtil.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. public class DateTimeUtil
  8. {
  9. /// <summary>
  10. /// 获取开始时间
  11. /// </summary>
  12. /// <param name="dateTime"></param>
  13. /// <param name="days"></param>
  14. /// <returns></returns>
  15. public static DateTime GetBeginTime(DateTime? dateTime, int days = 0)
  16. {
  17. if (dateTime == DateTime.MinValue || dateTime == null)
  18. return DateTime.Now.AddDays(days);
  19. return dateTime ?? DateTime.Now;
  20. }
  21. /// <summary>
  22. /// 时间戳转本地时间-时间戳精确到秒
  23. /// </summary>
  24. public static DateTime ToLocalTimeDateBySeconds(long unix)
  25. {
  26. return DateTimeOffset.FromUnixTimeSeconds(unix).ToLocalTime().DateTime;
  27. }
  28. /// <summary>
  29. /// 时间转时间戳Unix-时间戳精确到秒
  30. /// </summary>
  31. public static long ToUnixTimestampBySeconds(DateTime dt)
  32. {
  33. return new DateTimeOffset(dt).ToUnixTimeSeconds();
  34. }
  35. /// <summary>
  36. /// 时间戳转本地时间-时间戳精确到毫秒
  37. /// </summary>
  38. public static DateTime ToLocalTimeDateByMilliseconds(long unix)
  39. {
  40. return DateTimeOffset.FromUnixTimeMilliseconds(unix).ToLocalTime().DateTime;
  41. }
  42. /// <summary>
  43. /// 时间转时间戳Unix-时间戳精确到毫秒
  44. /// </summary>
  45. public static long ToUnixTimestampByMilliseconds(DateTime dt)
  46. {
  47. return new DateTimeOffset(dt).ToUnixTimeMilliseconds();
  48. }
  49. /// <summary>
  50. /// 毫秒转天时分秒
  51. /// </summary>
  52. /// <param name="ms"></param>
  53. /// <returns></returns>
  54. public static string FormatTime(long ms)
  55. {
  56. int ss = 1000;
  57. int mi = ss * 60;
  58. int hh = mi * 60;
  59. int dd = hh * 24;
  60. long day = ms / dd;
  61. long hour = (ms - day * dd) / hh;
  62. long minute = (ms - day * dd - hour * hh) / mi;
  63. long second = (ms - day * dd - hour * hh - minute * mi) / ss;
  64. long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
  65. string sDay = day < 10 ? "0" + day : "" + day; //天
  66. string sHour = hour < 10 ? "0" + hour : "" + hour;//小时
  67. string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
  68. string sSecond = second < 10 ? "0" + second : "" + second;//秒
  69. string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
  70. sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond;
  71. return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond);
  72. }
  73. /// <summary>
  74. /// 获取unix时间戳
  75. /// </summary>
  76. /// <param name="dt"></param>
  77. /// <returns></returns>
  78. public static long GetUnixTimeStamp(DateTime dt)
  79. {
  80. return ((DateTimeOffset)dt).ToUnixTimeMilliseconds();
  81. }
  82. /// <summary>
  83. /// 获取日期天的最小时间
  84. /// </summary>
  85. /// <param name="dt"></param>
  86. /// <returns></returns>
  87. public static DateTime GetDayMinDate(DateTime dt)
  88. {
  89. return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
  90. }
  91. /// <summary>
  92. /// 获取日期天的最大时间
  93. /// </summary>
  94. /// <param name="dt"></param>
  95. /// <returns></returns>
  96. public static DateTime GetDayMaxDate(DateTime dt)
  97. {
  98. return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
  99. }
  100. /// <summary>
  101. /// 获取日期天的最大时间
  102. /// </summary>
  103. /// <param name="dt"></param>
  104. /// <returns></returns>
  105. public static string FormatDateTime(DateTime? dt)
  106. {
  107. if (dt == null) return string.Empty;
  108. if (dt.Value.Year == DateTime.Now.Year)
  109. return dt.Value.ToString("MM-dd HH:mm");
  110. else
  111. return dt.Value.ToString("yyyy-MM-dd HH:mm");
  112. }
  113. /// <summary>
  114. /// 获取今天日期范围00:00:00 - 23:59:59
  115. /// </summary>
  116. /// <returns></returns>
  117. public static List<DateTime> GetTodayTimeList(DateTime time)
  118. {
  119. return new List<DateTime>
  120. {
  121. Convert.ToDateTime(time.ToString("D").ToString()),
  122. Convert.ToDateTime(time.AddDays(1).ToString("D").ToString()).AddSeconds(-1)
  123. };
  124. }
  125. /// <summary>
  126. /// 获取星期几
  127. /// </summary>
  128. /// <param name="dt"></param>
  129. /// <returns></returns>
  130. public static string GetWeekByDate(DateTime dt)
  131. {
  132. var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  133. return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
  134. }
  135. /// <summary>
  136. /// 获取这个月的第几周
  137. /// </summary>
  138. /// <param name="daytime"></param>
  139. /// <returns></returns>
  140. public static int GetWeekNumInMonth(DateTime daytime)
  141. {
  142. int dayInMonth = daytime.Day;
  143. // 本月第一天
  144. DateTime firstDay = daytime.AddDays(1 - daytime.Day);
  145. // 本月第一天是周几
  146. int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
  147. // 本月第一周有几天
  148. int firstWeekEndDay = 7 - (weekday - 1);
  149. // 当前日期和第一周之差
  150. int diffday = dayInMonth - firstWeekEndDay;
  151. diffday = diffday > 0 ? diffday : 1;
  152. // 当前是第几周,若整除7就减一天
  153. return ((diffday % 7) == 0 ? (diffday / 7 - 1) : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
  154. }
  155. }