DateUtil.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ET;
  8. namespace Hutool
  9. {
  10. public class DateUtil
  11. {
  12. public static int getAgeByBirth(string birth)
  13. {
  14. string yyear, mmonth, dday;
  15. yyear = birth.Substring(0, 4);
  16. mmonth = birth.Substring(4, 2);
  17. dday = birth.Substring(6, 2);
  18. int iyear = int.Parse(yyear);
  19. int imonth = int.Parse(mmonth);
  20. int iday = int.Parse(dday);
  21. DateTime birthTime = new DateTime(iyear, imonth, iday);
  22. return age(birthTime, DateTime.Now);
  23. }
  24. public static int thisYear()
  25. {
  26. return DateTime.Now.Year;
  27. }
  28. public static bool isLeapYear(int year)
  29. {
  30. return DateTime.IsLeapYear(year);
  31. }
  32. public static int age(DateTime birthday, DateTime dateToCompare)
  33. {
  34. int dYear = dateToCompare.Year - birthday.Year;
  35. DateTime birthdayThisYear = birthday.AddYears(dYear);
  36. if (dateToCompare.Ticks < birthdayThisYear.Ticks)
  37. {
  38. dYear--;
  39. }
  40. return dYear;
  41. }
  42. //从时间数组字符串里获取最近的一个开始时间,返回索引 leagueQuestionStartTime=10:10:00;19:10:00;20:10:00
  43. public static int GetNearestStartTimeIndex(List<string> leagueQuestionStartTime)
  44. {
  45. List<DateTime> startTimeList = new List<DateTime>();
  46. foreach (var timeStr in leagueQuestionStartTime)
  47. {
  48. DateTime time = DateTime.Parse(timeStr);
  49. startTimeList.Add(time);
  50. }
  51. int nearestIndex = 0;
  52. List<DateTime> nearestStartTimeList =
  53. startTimeList.Where(t => t.TimeOfDay > DateTime.Now.TimeOfDay).ToList();
  54. if (nearestStartTimeList.Count != 0)
  55. {
  56. DateTime nearestStartTime = nearestStartTimeList.Min();
  57. nearestIndex = startTimeList.IndexOf(nearestStartTime);
  58. }
  59. startTimeList.Clear();
  60. return nearestIndex;
  61. }
  62. //根据开服时间,获取近期的开月卡时间
  63. public static DateTime GetMonthlyCardStartTime(DateTime endDate, DateTime serverStartDate)
  64. {
  65. DateTime startDate = endDate.AddDays(-30); // 假设最近一次购买是在结束日期前30天
  66. while (startDate > serverStartDate)
  67. {
  68. TimeSpan difference = startDate - serverStartDate;
  69. if (difference.Days < 30)
  70. {
  71. break;
  72. }
  73. startDate = startDate.AddDays(-30); // 如果开始日期在开服日期之前,则往前推一个月卡周期
  74. }
  75. return startDate;
  76. }
  77. public static int CalculateDaysBetween(long startTimestamp, long endTimestamp)
  78. {
  79. // 将 Unix 时间戳转换为 DateTime
  80. DateTime startTime = DateTimeUtil.TimeStampToDateTime(startTimestamp);
  81. DateTime endTime = DateTimeUtil.TimeStampToDateTime(endTimestamp);
  82. // 计算调整后的 DateTime 对象之间的天数差异
  83. TimeSpan difference = endTime - startTime;
  84. int daysDifference = difference.Days;
  85. // 检查是否还有额外的时间差(小时、分钟、秒)
  86. if (difference.Hours > 0 || difference.Minutes > 0 || difference.Seconds > 0)
  87. {
  88. daysDifference++; // 如果有,天数差+1
  89. }
  90. return daysDifference;
  91. }
  92. private static DateTime AdjustDateTime(DateTime dateTime)
  93. {
  94. // 如果时间小于凌晨5点,则将日期减去一天
  95. if (dateTime.TimeOfDay < TimeSpan.FromHours(5))
  96. {
  97. dateTime = dateTime.AddDays(-1);
  98. }
  99. // 将时间调整为凌晨0点
  100. dateTime = dateTime.Date;
  101. return dateTime;
  102. }
  103. //按照凌晨5点为00点,获取当前月份
  104. public static int GetCurrentMonth()
  105. {
  106. DateTime currentDate = DateTime.Now;
  107. DateTime adjustedDate = currentDate.AddHours(-5); // 调整为当天上午5:00
  108. return adjustedDate.Month;
  109. }
  110. public static string GetSundayDateInt()
  111. {
  112. // 获取今天的日期
  113. DateTime today = DateTime.Today;
  114. // 计算今天到周日的天数差
  115. int daysUntilSunday = ((int)DayOfWeek.Sunday - (int)today.DayOfWeek + 7) % 7;
  116. // 获取这周的周日日期
  117. DateTime sunday = today.AddDays(daysUntilSunday);
  118. return sunday.ToString("yyyyMMdd");
  119. }
  120. public static int GetDateInt()
  121. {
  122. // 获取当前时间
  123. DateTime now = DateTime.Now;
  124. // 判断当前时间是否在凌晨5点之前
  125. if (now.Hour < 5)
  126. {
  127. // 如果在凌晨5点之前,返回昨天的日期
  128. DateTime yesterday = now.AddDays(-1);
  129. return Convert.ToInt32(yesterday.ToString("yyyyMMdd"));
  130. }
  131. else
  132. {
  133. // 否则返回今天的日期
  134. return Convert.ToInt32(now.ToString("yyyyMMdd"));
  135. }
  136. }
  137. /// <summary>
  138. /// 年月日 解析到某个时间点
  139. /// </summary>
  140. /// <param name="weekDate"></param>
  141. /// <param name="cfgClearingEndTime"></param>
  142. /// <returns></returns>
  143. /// <exception cref="Exception"></exception>
  144. public static DateTime GetDateTime(int weekDate, string cfgClearingEndTime)
  145. {
  146. // 将整数型日期转换为字符串形式
  147. string dateStr = weekDate.ToString();
  148. // 完整的日期时间字符串
  149. string dateTimeStr = dateStr + cfgClearingEndTime;
  150. // 解析成 DateTime 对象
  151. if (DateTime.TryParseExact(dateTimeStr.Trim(), "yyyyMMddHH:mm:ss", CultureInfo.InvariantCulture,
  152. DateTimeStyles.None, out var dateTime))
  153. {
  154. return dateTime;
  155. }
  156. throw new Exception("无法解析提供的日期时间字符串");
  157. }
  158. }
  159. }