DateTimeHelper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using Masuit.Tools.Models;
  8. namespace Masuit.Tools.DateTimeExt
  9. {
  10. /// <summary>
  11. /// 日期操作工具类
  12. /// </summary>
  13. public static class DateTimeHelper
  14. {
  15. /// <summary>
  16. /// 获取某一年有多少周
  17. /// </summary>
  18. /// <param name="now"></param>
  19. /// <returns>该年周数</returns>
  20. public static int GetWeekAmount(this in DateTime now)
  21. {
  22. var end = new DateTime(now.Year, 12, 31); //该年最后一天
  23. var gc = new GregorianCalendar();
  24. return gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday); //该年星期数
  25. }
  26. /// <summary>
  27. /// 返回年度第几个星期 默认星期日是第一天
  28. /// </summary>
  29. /// <param name="date">时间</param>
  30. /// <returns>第几周</returns>
  31. public static int WeekOfYear(this in DateTime date)
  32. {
  33. var gc = new GregorianCalendar();
  34. return gc.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
  35. }
  36. /// <summary>
  37. /// 返回年度第几个星期
  38. /// </summary>
  39. /// <param name="date">时间</param>
  40. /// <param name="week">一周的开始日期</param>
  41. /// <returns>第几周</returns>
  42. public static int WeekOfYear(this in DateTime date, DayOfWeek week)
  43. {
  44. var gc = new GregorianCalendar();
  45. return gc.GetWeekOfYear(date, CalendarWeekRule.FirstDay, week);
  46. }
  47. /// <summary>
  48. /// 得到一年中的某周的起始日和截止日
  49. /// 周数 nNumWeek
  50. /// </summary>
  51. /// <param name="now"></param>
  52. /// <param name="nNumWeek">第几周</param>
  53. public static DateTimeRange GetWeekTime(this DateTime now, int nNumWeek)
  54. {
  55. var dt = new DateTime(now.Year, 1, 1);
  56. dt += new TimeSpan((nNumWeek - 1) * 7, 0, 0, 0);
  57. return new DateTimeRange(dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday), dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1));
  58. }
  59. /// <summary>
  60. /// 得到当前周的起始日和截止日
  61. /// </summary>
  62. /// <param name="dt"></param>
  63. public static DateTimeRange GetCurrentWeek(this DateTime dt)
  64. {
  65. return new DateTimeRange(dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday).Date, dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1).Date.AddSeconds(86399));
  66. }
  67. /// <summary>
  68. /// 得到当前月的起始日和截止日
  69. /// </summary>
  70. /// <param name="dt"></param>
  71. public static DateTimeRange GetCurrentMonth(this DateTime dt)
  72. {
  73. return new DateTimeRange(new DateTime(dt.Year, dt.Month, 1), new DateTime(dt.Year, dt.Month, GetDaysOfMonth(dt), 23, 59, 59));
  74. }
  75. /// <summary>
  76. /// 得到当前农历月的起始日和截止日
  77. /// </summary>
  78. /// <param name="dt"></param>
  79. public static DateTimeRange GetCurrentLunarMonth(this DateTime dt)
  80. {
  81. var calendar = new ChineseCalendar(dt);
  82. return new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, calendar.ChineseMonth, 1).Date, new ChineseCalendar(calendar.ChineseYear, calendar.ChineseMonth, calendar.GetChineseMonthDays()).Date.AddSeconds(86399));
  83. }
  84. /// <summary>
  85. /// 得到当前年的起始日和截止日
  86. /// </summary>
  87. /// <param name="dt"></param>
  88. public static DateTimeRange GetCurrentYear(this DateTime dt)
  89. {
  90. return new DateTimeRange(new DateTime(dt.Year, 1, 1), new DateTime(dt.Year, 12, 31, 23, 59, 59));
  91. }
  92. /// <summary>
  93. /// 得到当前农历年的起始日和截止日
  94. /// </summary>
  95. /// <param name="dt"></param>
  96. public static DateTimeRange GetCurrentLunarYear(this DateTime dt)
  97. {
  98. var calendar = new ChineseCalendar(dt);
  99. return new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, 1, 1).Date, new ChineseCalendar(calendar.ChineseYear, 12, calendar.GetChineseMonthDays(calendar.ChineseYear, 12)).Date.AddSeconds(86399));
  100. }
  101. /// <summary>
  102. /// 得到当前季度的起始日和截止日
  103. /// </summary>
  104. /// <param name="dt"></param>
  105. public static DateTimeRange GetCurrentQuarter(this DateTime dt)
  106. {
  107. return dt.Month switch
  108. {
  109. >= 1 and <= 3 => new DateTimeRange(new DateTime(dt.Year, 1, 1), new DateTime(dt.Year, 3, 31, 23, 59, 59)),
  110. >= 4 and <= 6 => new DateTimeRange(new DateTime(dt.Year, 4, 1), new DateTime(dt.Year, 6, 30, 23, 59, 59)),
  111. >= 7 and <= 9 => new DateTimeRange(new DateTime(dt.Year, 7, 1), new DateTime(dt.Year, 9, 30, 23, 59, 59)),
  112. >= 10 and <= 12 => new DateTimeRange(new DateTime(dt.Year, 10, 1), new DateTime(dt.Year, 12, 31, 23, 59, 59)),
  113. _ => throw new ArgumentOutOfRangeException()
  114. };
  115. }
  116. /// <summary>
  117. /// 得到当前农历季度的起始日和截止日
  118. /// </summary>
  119. /// <param name="dt"></param>
  120. public static DateTimeRange GetCurrentLunarQuarter(this DateTime dt)
  121. {
  122. var calendar = new ChineseCalendar(dt);
  123. return dt.Month switch
  124. {
  125. >= 1 and <= 3 => new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, 1, 1).Date, new ChineseCalendar(calendar.ChineseYear, 3, calendar.GetChineseMonthDays(calendar.ChineseYear, 3)).Date.AddSeconds(86399)),
  126. >= 4 and <= 6 => new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, 4, 1).Date, new ChineseCalendar(calendar.ChineseYear, 6, calendar.GetChineseMonthDays(calendar.ChineseYear, 6)).Date.AddSeconds(86399)),
  127. >= 7 and <= 9 => new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, 7, 1).Date, new ChineseCalendar(calendar.ChineseYear, 9, calendar.GetChineseMonthDays(calendar.ChineseYear, 9)).Date.AddSeconds(86399)),
  128. >= 10 and <= 12 => new DateTimeRange(new ChineseCalendar(calendar.ChineseYear, 10, 1).Date, new ChineseCalendar(calendar.ChineseYear, 12, calendar.GetChineseMonthDays(calendar.ChineseYear, 12)).Date.AddSeconds(86399)),
  129. _ => throw new ArgumentOutOfRangeException()
  130. };
  131. }
  132. /// <summary>
  133. /// 得到当前农历季度的起始日和截止日
  134. /// </summary>
  135. /// <param name="dt"></param>
  136. public static DateTimeRange GetCurrentSolar(this DateTime dt)
  137. {
  138. var calendar = new ChineseCalendar(dt);
  139. ChineseCalendar[] quarters =
  140. [
  141. calendar.ChineseTwentyFourPrevDay.ChineseTwentyFourPrevDay.ChineseTwentyFourPrevDay,
  142. calendar.ChineseTwentyFourPrevDay.ChineseTwentyFourPrevDay,
  143. calendar.ChineseTwentyFourPrevDay,
  144. calendar,
  145. calendar.ChineseTwentyFourNextDay,
  146. calendar.ChineseTwentyFourNextDay.ChineseTwentyFourNextDay,
  147. calendar.ChineseTwentyFourNextDay.ChineseTwentyFourNextDay.ChineseTwentyFourNextDay
  148. ];
  149. var solar = quarters.LastOrDefault(c => new[] { "春分", "夏至", "秋分", "冬至" }.Contains(c.ChineseTwentyFourDay));
  150. var start = solar.ChineseTwentyFourPrevDay.ChineseTwentyFourPrevDay.ChineseTwentyFourPrevDay;
  151. var end = solar.ChineseTwentyFourNextDay.ChineseTwentyFourNextDay.ChineseTwentyFourNextDay;
  152. return new DateTimeRange(start.Date, end.Date.AddSeconds(-1));
  153. }
  154. /// <summary>
  155. /// 得到当前范围的起始日和截止日
  156. /// </summary>
  157. /// <param name="dt"></param>
  158. /// <param name="type"></param>
  159. public static DateTimeRange GetCurrentRange(this DateTime dt, DateRangeType type)
  160. {
  161. return type switch
  162. {
  163. DateRangeType.Week => GetCurrentWeek(dt),
  164. DateRangeType.Month => GetCurrentMonth(dt),
  165. DateRangeType.Quarter => GetCurrentQuarter(dt),
  166. DateRangeType.Year => GetCurrentYear(dt),
  167. DateRangeType.LunarMonth => GetCurrentLunarMonth(dt),
  168. DateRangeType.LunarQuarter => GetCurrentLunarQuarter(dt),
  169. DateRangeType.Solar => GetCurrentSolar(dt),
  170. DateRangeType.LunarYear => GetCurrentLunarYear(dt),
  171. _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
  172. };
  173. }
  174. #region P/Invoke 设置本地时间
  175. [DllImport("kernel32.dll")]
  176. private static extern bool SetLocalTime(ref SystemTime time);
  177. [StructLayout(LayoutKind.Sequential)]
  178. private record struct SystemTime
  179. {
  180. public short year;
  181. public short month;
  182. public short dayOfWeek;
  183. public short day;
  184. public short hour;
  185. public short minute;
  186. public short second;
  187. public short milliseconds;
  188. }
  189. /// <summary>
  190. /// 设置本地计算机系统时间,仅支持Windows系统
  191. /// </summary>
  192. /// <param name="dt">DateTime对象</param>
  193. public static void SetLocalTime(this in DateTime dt)
  194. {
  195. SystemTime st;
  196. st.year = (short)dt.Year;
  197. st.month = (short)dt.Month;
  198. st.dayOfWeek = (short)dt.DayOfWeek;
  199. st.day = (short)dt.Day;
  200. st.hour = (short)dt.Hour;
  201. st.minute = (short)dt.Minute;
  202. st.second = (short)dt.Second;
  203. st.milliseconds = (short)dt.Millisecond;
  204. SetLocalTime(ref st);
  205. }
  206. #endregion P/Invoke 设置本地时间
  207. /// <summary>
  208. /// 返回相对于当前时间的相对天数
  209. /// </summary>
  210. /// <param name="dt"></param>
  211. /// <param name="relativeday">相对天数</param>
  212. public static string GetDateTime(this in DateTime dt, int relativeday)
  213. {
  214. return dt.AddDays(relativeday).ToString("yyyy-MM-dd HH:mm:ss");
  215. }
  216. /// <summary>
  217. /// 获取该时间相对于1970-01-01T00:00:00Z的秒数
  218. /// </summary>
  219. /// <param name="dt"></param>
  220. /// <returns></returns>
  221. public static long GetTotalSeconds(this in DateTime dt) => new DateTimeOffset(dt).UtcDateTime.Ticks / 10_000_000L - 62135596800L;
  222. /// <summary>
  223. /// 获取该时间相对于1970-01-01T00:00:00Z的毫秒数
  224. /// </summary>
  225. /// <param name="dt"></param>
  226. /// <returns></returns>
  227. public static long GetTotalMilliseconds(this in DateTime dt) => new DateTimeOffset(dt).UtcDateTime.Ticks / 10000L - 62135596800000L;
  228. /// <summary>
  229. /// 获取该时间相对于1970-01-01T00:00:00Z的微秒时间戳
  230. /// </summary>
  231. /// <param name="dt"></param>
  232. /// <returns></returns>
  233. public static long GetTotalMicroseconds(this in DateTime dt) => (new DateTimeOffset(dt).UtcTicks - 621355968000000000) / 10;
  234. [DllImport("Kernel32.dll")]
  235. private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
  236. /// <summary>
  237. /// 获取该时间相对于1970-01-01T00:00:00Z的纳秒时间戳
  238. /// </summary>
  239. /// <param name="dt"></param>
  240. /// <returns></returns>
  241. public static long GetTotalNanoseconds(this in DateTime dt)
  242. {
  243. var ticks = (new DateTimeOffset(dt).UtcTicks - 621355968000000000) * 100;
  244. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  245. {
  246. QueryPerformanceCounter(out var timestamp);
  247. return ticks + timestamp % 100;
  248. }
  249. return ticks + Stopwatch.GetTimestamp() % 100;
  250. }
  251. /// <summary>
  252. /// 获取该时间相对于1970-01-01T00:00:00Z的分钟数
  253. /// </summary>
  254. /// <param name="dt"></param>
  255. /// <returns></returns>
  256. public static double GetTotalMinutes(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalMinutes;
  257. /// <summary>
  258. /// 获取该时间相对于1970-01-01T00:00:00Z的小时数
  259. /// </summary>
  260. /// <param name="dt"></param>
  261. /// <returns></returns>
  262. public static double GetTotalHours(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalHours;
  263. /// <summary>
  264. /// 获取该时间相对于1970-01-01T00:00:00Z的天数
  265. /// </summary>
  266. /// <param name="dt"></param>
  267. /// <returns></returns>
  268. public static double GetTotalDays(this in DateTime dt) => new DateTimeOffset(dt).Offset.TotalDays;
  269. /// <summary>本年有多少天</summary>
  270. /// <param name="dt">日期</param>
  271. /// <returns>本天在当年的天数</returns>
  272. public static int GetDaysOfYear(this in DateTime dt)
  273. {
  274. //取得传入参数的年份部分,用来判断是否是闰年
  275. int n = dt.Year;
  276. return DateTime.IsLeapYear(n) ? 366 : 365;
  277. }
  278. /// <summary>本月有多少天</summary>
  279. /// <param name="now"></param>
  280. /// <returns>天数</returns>
  281. public static int GetDaysOfMonth(this DateTime now)
  282. {
  283. return now.Month switch
  284. {
  285. 1 => 31,
  286. 2 => DateTime.IsLeapYear(now.Year) ? 29 : 28,
  287. 3 => 31,
  288. 4 => 30,
  289. 5 => 31,
  290. 6 => 30,
  291. 7 => 31,
  292. 8 => 31,
  293. 9 => 30,
  294. 10 => 31,
  295. 11 => 30,
  296. 12 => 31,
  297. _ => 0
  298. };
  299. }
  300. /// <summary>返回当前日期的星期名称</summary>
  301. /// <param name="now">日期</param>
  302. /// <returns>星期名称</returns>
  303. public static string GetWeekNameOfDay(this in DateTime now)
  304. {
  305. return now.DayOfWeek switch
  306. {
  307. DayOfWeek.Monday => "星期一",
  308. DayOfWeek.Tuesday => "星期二",
  309. DayOfWeek.Wednesday => "星期三",
  310. DayOfWeek.Thursday => "星期四",
  311. DayOfWeek.Friday => "星期五",
  312. DayOfWeek.Saturday => "星期六",
  313. DayOfWeek.Sunday => "星期日",
  314. _ => ""
  315. };
  316. }
  317. /// <summary>
  318. /// 判断时间是否在区间内
  319. /// </summary>
  320. /// <param name="this"></param>
  321. /// <param name="start">开始</param>
  322. /// <param name="end">结束</param>
  323. /// <param name="mode">模式</param>
  324. /// <returns></returns>
  325. public static bool In(this in DateTime @this, DateTime start, DateTime end, RangeMode mode = RangeMode.Close)
  326. {
  327. return mode switch
  328. {
  329. RangeMode.Open => start < @this && end > @this,
  330. RangeMode.Close => start <= @this && end >= @this,
  331. RangeMode.OpenClose => start < @this && end >= @this,
  332. RangeMode.CloseOpen => start <= @this && end > @this,
  333. _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
  334. };
  335. }
  336. /// <summary>
  337. /// 返回某年某月最后一天
  338. /// </summary>
  339. /// <param name="now"></param>
  340. /// <returns>日</returns>
  341. public static int GetMonthLastDate(this DateTime now)
  342. {
  343. DateTime lastDay = new DateTime(now.Year, now.Month, new GregorianCalendar().GetDaysInMonth(now.Year, now.Month));
  344. return lastDay.Day;
  345. }
  346. /// <summary>
  347. /// 获得一段时间内有多少小时
  348. /// </summary>
  349. /// <param name="start">起始时间</param>
  350. /// <param name="end">终止时间</param>
  351. /// <returns>小时差</returns>
  352. public static string GetTimeDelay(this in DateTime start, DateTime end)
  353. {
  354. return (end - start).ToString("c");
  355. }
  356. /// <summary>
  357. /// 返回时间差
  358. /// </summary>
  359. /// <param name="dateTime1">时间1</param>
  360. /// <param name="dateTime2">时间2</param>
  361. /// <returns>时间差</returns>
  362. public static string DateDiff(this in DateTime dateTime1, in DateTime dateTime2)
  363. {
  364. string dateDiff;
  365. var ts = dateTime2 - dateTime1;
  366. if (ts.TotalDays >= 1)
  367. {
  368. dateDiff = ts.TotalDays >= 30 ? (ts.TotalDays / 30) + "个月前" : ts.TotalDays + "天前";
  369. }
  370. else
  371. {
  372. dateDiff = ts.Hours > 1 ? ts.Hours + "小时前" : ts.Minutes + "分钟前";
  373. }
  374. return dateDiff;
  375. }
  376. /// <summary>
  377. /// 计算2个时间差
  378. /// </summary>
  379. /// <param name="beginTime">开始时间</param>
  380. /// <param name="endTime">结束时间</param>
  381. /// <returns>时间差</returns>
  382. public static string GetDiffTime(this in DateTime beginTime, in DateTime endTime)
  383. {
  384. string strResout = string.Empty;
  385. //获得2时间的时间间隔秒计算
  386. TimeSpan span = endTime.Subtract(beginTime);
  387. if (span.Days >= 365)
  388. {
  389. strResout += span.Days / 365 + "年";
  390. }
  391. if (span.Days >= 30)
  392. {
  393. strResout += span.Days % 365 / 30 + "个月";
  394. }
  395. if (span.Days >= 1)
  396. {
  397. strResout += (int)(span.TotalDays % 30.42) + "天";
  398. }
  399. if (span.Hours >= 1)
  400. {
  401. strResout += span.Hours + "小时";
  402. }
  403. if (span.Minutes >= 1)
  404. {
  405. strResout += span.Minutes + "分钟";
  406. }
  407. if (span.Seconds >= 1)
  408. {
  409. strResout += span.Seconds + "秒";
  410. }
  411. return strResout;
  412. }
  413. /// <summary>
  414. /// 根据某个时间段查找在某批时间段中的最大并集
  415. /// </summary>
  416. /// <param name="destination"></param>
  417. /// <param name="sources"></param>
  418. /// <typeparam name="T"></typeparam>
  419. /// <returns></returns>
  420. public static ICollection<T> GetUnionSet<T>(this T destination, List<T> sources) where T : ITimePeriod, new()
  421. {
  422. var result = true;
  423. ICollection<T> frames = new List<T>();
  424. var timeFrames = sources.Where(frame =>
  425. !(destination.Start > frame.End || destination.End < frame.Start)).ToList();
  426. if (timeFrames.Any())
  427. foreach (var frame in timeFrames)
  428. {
  429. frames.Add(frame);
  430. sources.Remove(frame);
  431. }
  432. if (!frames.Any()) return frames;
  433. var timePeriod = new T()
  434. {
  435. End = frames.OrderBy(frame => frame.End).Max(frame => frame.End),
  436. Start = frames.OrderBy(frame => frame.Start).Min(frame => frame.Start)
  437. };
  438. while (result)
  439. {
  440. var maxTimeFrame = GetUnionSet<T>(timePeriod, sources);
  441. if (!maxTimeFrame.Any())
  442. result = false;
  443. else
  444. foreach (var frame in maxTimeFrame)
  445. frames.Add(frame);
  446. }
  447. return frames;
  448. }
  449. /// <summary>
  450. /// 获取一批时间段内存在相互重叠的最大时间段
  451. /// </summary>
  452. /// <param name="destination">基础时间段</param>
  453. /// <param name="sources">一批时间段</param>
  454. /// <typeparam name="T"></typeparam>
  455. /// <returns></returns>
  456. /// <remarks>源数据sources 会受到影响</remarks>
  457. public static T GetMaxTimePeriod<T>(this T destination, List<T> sources) where T : ITimePeriod, new()
  458. {
  459. var list = sources.Select(period => new T()
  460. {
  461. End = period.End,
  462. Start = period.Start,
  463. }).ToList();
  464. var timePeriods = GetUnionSet(destination, list);
  465. return new T()
  466. {
  467. End = timePeriods.OrderBy(period => period.End).Max(period => period.End),
  468. Start = timePeriods.OrderBy(period => period.Start).Min(period => period.Start)
  469. };
  470. }
  471. }
  472. /// <summary>
  473. ///
  474. /// </summary>
  475. public interface ITimePeriod
  476. {
  477. /// <summary>
  478. /// 起始时间
  479. /// </summary>
  480. public DateTime Start { get; set; }
  481. /// <summary>
  482. /// 终止时间
  483. /// </summary>
  484. public DateTime End { get; set; }
  485. }
  486. /// <summary>
  487. /// 区间模式
  488. /// </summary>
  489. public enum RangeMode
  490. {
  491. /// <summary>
  492. /// 开区间
  493. /// </summary>
  494. Open,
  495. /// <summary>
  496. /// 闭区间
  497. /// </summary>
  498. Close,
  499. /// <summary>
  500. /// 左开右闭区间
  501. /// </summary>
  502. OpenClose,
  503. /// <summary>
  504. /// 左闭右开区间
  505. /// </summary>
  506. CloseOpen
  507. }
  508. public enum DateRangeType
  509. {
  510. Week,
  511. Month,
  512. Quarter,
  513. Year,
  514. LunarMonth,
  515. LunarQuarter,
  516. Solar,
  517. LunarYear,
  518. }
  519. }