DateTimeUtil.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core;
  10. public class DateTimeUtil
  11. {
  12. /// <summary>
  13. /// 获取开始时间
  14. /// </summary>
  15. /// <param name="dateTime"></param>
  16. /// <param name="days"></param>
  17. /// <returns></returns>
  18. public static DateTime GetBeginTime(DateTime? dateTime, int days = 0)
  19. {
  20. if (dateTime == DateTime.MinValue || dateTime == null)
  21. return DateTime.Now.AddDays(days);
  22. return dateTime ?? DateTime.Now;
  23. }
  24. /// <summary>
  25. /// 时间戳转本地时间-时间戳精确到秒
  26. /// </summary>
  27. public static DateTime ToLocalTimeDateBySeconds(long unix)
  28. {
  29. return DateTimeOffset.FromUnixTimeSeconds(unix).ToLocalTime().DateTime;
  30. }
  31. /// <summary>
  32. /// 时间转时间戳Unix-时间戳精确到秒
  33. /// </summary>
  34. public static long ToUnixTimestampBySeconds(DateTime dt)
  35. {
  36. return new DateTimeOffset(dt).ToUnixTimeSeconds();
  37. }
  38. /// <summary>
  39. /// 时间戳转本地时间-时间戳精确到毫秒
  40. /// </summary>
  41. public static DateTime ToLocalTimeDateByMilliseconds(long unix)
  42. {
  43. return DateTimeOffset.FromUnixTimeMilliseconds(unix).ToLocalTime().DateTime;
  44. }
  45. /// <summary>
  46. /// 时间转时间戳Unix-时间戳精确到毫秒
  47. /// </summary>
  48. public static long ToUnixTimestampByMilliseconds(DateTime dt)
  49. {
  50. return new DateTimeOffset(dt).ToUnixTimeMilliseconds();
  51. }
  52. /// <summary>
  53. /// 毫秒转天时分秒
  54. /// </summary>
  55. /// <param name="ms"></param>
  56. /// <returns></returns>
  57. public static string FormatTime(long ms)
  58. {
  59. int ss = 1000;
  60. int mi = ss * 60;
  61. int hh = mi * 60;
  62. int dd = hh * 24;
  63. long day = ms / dd;
  64. long hour = (ms - day * dd) / hh;
  65. long minute = (ms - day * dd - hour * hh) / mi;
  66. long second = (ms - day * dd - hour * hh - minute * mi) / ss;
  67. long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
  68. string sDay = day < 10 ? "0" + day : "" + day; //天
  69. string sHour = hour < 10 ? "0" + hour : "" + hour;//小时
  70. string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
  71. string sSecond = second < 10 ? "0" + second : "" + second;//秒
  72. string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
  73. sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond;
  74. return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond);
  75. }
  76. /// <summary>
  77. /// 获取unix时间戳
  78. /// </summary>
  79. /// <param name="dt"></param>
  80. /// <returns></returns>
  81. public static long GetUnixTimeStamp(DateTime dt)
  82. {
  83. return ((DateTimeOffset)dt).ToUnixTimeMilliseconds();
  84. }
  85. /// <summary>
  86. /// 获取日期天的最小时间
  87. /// </summary>
  88. /// <param name="dt"></param>
  89. /// <returns></returns>
  90. public static DateTime GetDayMinDate(DateTime dt)
  91. {
  92. return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
  93. }
  94. /// <summary>
  95. /// 获取日期天的最大时间
  96. /// </summary>
  97. /// <param name="dt"></param>
  98. /// <returns></returns>
  99. public static DateTime GetDayMaxDate(DateTime dt)
  100. {
  101. return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
  102. }
  103. /// <summary>
  104. /// 获取日期天的最大时间
  105. /// </summary>
  106. /// <param name="dt"></param>
  107. /// <returns></returns>
  108. public static string FormatDateTime(DateTime? dt)
  109. {
  110. if (dt == null) return string.Empty;
  111. if (dt.Value.Year == DateTime.Now.Year)
  112. return dt.Value.ToString("MM-dd HH:mm");
  113. else
  114. return dt.Value.ToString("yyyy-MM-dd HH:mm");
  115. }
  116. /// <summary>
  117. /// 获取今天日期范围00:00:00 - 23:59:59
  118. /// </summary>
  119. /// <returns></returns>
  120. public static List<DateTime> GetTodayTimeList(DateTime time)
  121. {
  122. return new List<DateTime>
  123. {
  124. Convert.ToDateTime(time.ToString("D").ToString()),
  125. Convert.ToDateTime(time.AddDays(1).ToString("D").ToString()).AddSeconds(-1)
  126. };
  127. }
  128. /// <summary>
  129. /// 获取星期几
  130. /// </summary>
  131. /// <param name="dt"></param>
  132. /// <returns></returns>
  133. public static string GetWeekByDate(DateTime dt)
  134. {
  135. var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  136. return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
  137. }
  138. /// <summary>
  139. /// 获取这个月的第几周
  140. /// </summary>
  141. /// <param name="daytime"></param>
  142. /// <returns></returns>
  143. public static int GetWeekNumInMonth(DateTime daytime)
  144. {
  145. int dayInMonth = daytime.Day;
  146. // 本月第一天
  147. DateTime firstDay = daytime.AddDays(1 - daytime.Day);
  148. // 本月第一天是周几
  149. int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
  150. // 本月第一周有几天
  151. int firstWeekEndDay = 7 - (weekday - 1);
  152. // 当前日期和第一周之差
  153. int diffday = dayInMonth - firstWeekEndDay;
  154. diffday = diffday > 0 ? diffday : 1;
  155. // 当前是第几周,若整除7就减一天
  156. return ((diffday % 7) == 0 ? (diffday / 7 - 1) : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
  157. }
  158. }