DateTimeExtension.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ERP.Framework.Constants;
  2. namespace System
  3. {
  4. public static class DateTimeExtension
  5. {
  6. /// <summary>
  7. /// 获取当前日期时间与指定日期时间之间的年龄差
  8. /// </summary>
  9. /// <param name="this"> 指定日期 </param>
  10. /// <returns> 返回两个日期之间的年龄差 </returns>
  11. public static int GetAge(this DateTime @this)
  12. {
  13. //如果当年月份小于指定日期的月份,
  14. //或者当年月份等于指定日期的月份但当年日期小于指定日期的日期,
  15. //则返回当前年份与指定日期年份之间的差值减一
  16. if (DateTime.Today.Month < @this.Month ||
  17. DateTime.Today.Month == @this.Month &&
  18. DateTime.Today.Day < @this.Day)
  19. {
  20. return DateTime.Today.Year - @this.Year - 1;
  21. }
  22. // 返回当前年份与指定日期年份之间的差值
  23. return DateTime.Today.Year - @this.Year;
  24. }
  25. /// <summary>
  26. /// 判断当前日期是否为工作日
  27. /// </summary>
  28. /// <param name="this"> 要进行判断的日期 </param>
  29. /// <returns> 当前日期是工作日返回 true ,当前日期是周末返回 false</returns>
  30. public static bool IsWeekDay(this DateTime @this)
  31. {
  32. return !(@this.DayOfWeek == DayOfWeek.Saturday || @this.DayOfWeek == DayOfWeek.Sunday);
  33. }
  34. /// <summary>
  35. /// 判断当前日期是否为周末
  36. /// </summary>
  37. /// <param name="this"> 要进行判断的日期 </param>
  38. /// <returns> 当前日期是周末返回 true ,当前日期是工作日返回 false </returns>
  39. public static bool IsWeekendDay(this DateTime @this)
  40. {
  41. return @this.DayOfWeek == DayOfWeek.Saturday || @this.DayOfWeek == DayOfWeek.Sunday;
  42. }
  43. /// <summary>
  44. /// 获取当前日期所在星期的第一天,即本周的起始日期
  45. /// </summary>
  46. /// <param name="dt"> 要进行处理的日期 </param>
  47. /// <param name="startDayOfWeek"> 星期的起始日,默认为星期天 </param>
  48. /// <returns> 返回本周的起始日期 </returns>
  49. public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startDayOfWeek = DayOfWeek.Sunday)
  50. {
  51. var start = new DateTime(dt.Year, dt.Month, dt.Day);
  52. if (start.DayOfWeek != startDayOfWeek)
  53. {
  54. var d = startDayOfWeek - start.DayOfWeek;
  55. if (startDayOfWeek <= start.DayOfWeek)
  56. {
  57. return start.AddDays(d);
  58. }
  59. return start.AddDays(-7 + d);
  60. }
  61. return start;
  62. }
  63. /// <summary>
  64. /// 将当前日期时间转换为 Unix 时间戳(以秒为单位)
  65. /// </summary>
  66. /// <param name="this"> 要进行转换的日期时间 </param>
  67. /// <returns> 返回 Unix 时间戳 </returns>
  68. public static TimeSpan ToEpochTimeSpan(this DateTime @this) => @this.ToUniversalTime().Subtract(new DateTime(1970, 1, 1));
  69. /// <summary>
  70. /// 判断当前日期是否在指定日期范围内
  71. /// </summary>
  72. /// <param name="this"> 要进行判断的日期< </param>
  73. /// <param name="minValue"> 日期范围的最小值 </param>
  74. /// <param name="maxValue"> 日期范围的最大值 </param>
  75. /// <returns>当前日期在指定的日期范围内返回 true ,当前日期不在指定的日期范围内返回 false</returns>
  76. public static bool InRange(this DateTime @this, DateTime minValue, DateTime maxValue)
  77. {
  78. return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0;
  79. }
  80. /// <summary>
  81. /// 返回日期字符串 (yyyy/MM/dd)
  82. /// </summary>
  83. /// <param name="this"> 当前日期 </param>
  84. /// <returns> 日期字符串 </returns>
  85. public static string ToDateString(this DateTime @this)
  86. {
  87. return @this.ToString(DateTimeConstant.DATE);
  88. }
  89. /// <summary>
  90. /// 返回日期字符串 (yyyy/MM/dd HH:mm)
  91. /// </summary>
  92. /// <param name="this"> 当前日期 </param>
  93. /// <returns> 日期字符串 </returns>
  94. public static string ToDateShortString(this DateTime @this)
  95. {
  96. return @this.ToString(DateTimeConstant.DATE_SHORT);
  97. }
  98. /// <summary>
  99. /// 返回日期字符串 (yyyy/MM/dd HH:mm:ss)
  100. /// </summary>
  101. /// <param name="this"> 当前日期 </param>
  102. /// <returns> 日期字符串 </returns>
  103. public static string ToDateLongString(this DateTime @this)
  104. {
  105. return @this.ToString(DateTimeConstant.DATE_LONG);
  106. }
  107. /// <summary>
  108. /// 返回日期字符串 (yyyy-MM-dd)
  109. /// </summary>
  110. /// <param name="this"> 当前日期 </param>
  111. /// <returns> 日期字符串 </returns>
  112. public static string ToDateTimeString(this DateTime @this)
  113. {
  114. return @this.ToString(DateTimeConstant.DETE_TIME);
  115. }
  116. /// <summary>
  117. /// 返回日期字符串 (yyyy-MM-dd HH:mm)
  118. /// </summary>
  119. /// <param name="this"> 当前日期 </param>
  120. /// <returns> 日期字符串 </returns>
  121. public static string ToDateTimeShortString(this DateTime @this)
  122. {
  123. return @this.ToString(DateTimeConstant.DETE_TIME_SHORT);
  124. }
  125. /// <summary>
  126. /// 返回日期字符串 (yyyy-MM-dd HH:mm:ss)
  127. /// </summary>
  128. /// <param name="this"> 当前日期 </param>
  129. /// <returns> 日期字符串 </returns>
  130. public static string ToDateTimeLongString(this DateTime @this)
  131. {
  132. return @this.ToString(DateTimeConstant.DETE_TIME_LONG);
  133. }
  134. }
  135. }