| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ET;
- namespace Hutool
- {
- public class DateUtil
- {
- public static int getAgeByBirth(string birth)
- {
- string yyear, mmonth, dday;
- yyear = birth.Substring(0, 4);
- mmonth = birth.Substring(4, 2);
- dday = birth.Substring(6, 2);
- int iyear = int.Parse(yyear);
- int imonth = int.Parse(mmonth);
- int iday = int.Parse(dday);
- DateTime birthTime = new DateTime(iyear, imonth, iday);
- return age(birthTime, DateTime.Now);
- }
- public static int thisYear()
- {
- return DateTime.Now.Year;
- }
- public static bool isLeapYear(int year)
- {
- return DateTime.IsLeapYear(year);
- }
- public static int age(DateTime birthday, DateTime dateToCompare)
- {
- int dYear = dateToCompare.Year - birthday.Year;
- DateTime birthdayThisYear = birthday.AddYears(dYear);
- if (dateToCompare.Ticks < birthdayThisYear.Ticks)
- {
- dYear--;
- }
- return dYear;
- }
- //从时间数组字符串里获取最近的一个开始时间,返回索引 leagueQuestionStartTime=10:10:00;19:10:00;20:10:00
- public static int GetNearestStartTimeIndex(List<string> leagueQuestionStartTime)
- {
- List<DateTime> startTimeList = new List<DateTime>();
- foreach (var timeStr in leagueQuestionStartTime)
- {
- DateTime time = DateTime.Parse(timeStr);
- startTimeList.Add(time);
- }
- int nearestIndex = 0;
- List<DateTime> nearestStartTimeList =
- startTimeList.Where(t => t.TimeOfDay > DateTime.Now.TimeOfDay).ToList();
- if (nearestStartTimeList.Count != 0)
- {
- DateTime nearestStartTime = nearestStartTimeList.Min();
- nearestIndex = startTimeList.IndexOf(nearestStartTime);
- }
- startTimeList.Clear();
- return nearestIndex;
- }
- //根据开服时间,获取近期的开月卡时间
- public static DateTime GetMonthlyCardStartTime(DateTime endDate, DateTime serverStartDate)
- {
- DateTime startDate = endDate.AddDays(-30); // 假设最近一次购买是在结束日期前30天
- while (startDate > serverStartDate)
- {
- TimeSpan difference = startDate - serverStartDate;
- if (difference.Days < 30)
- {
- break;
- }
- startDate = startDate.AddDays(-30); // 如果开始日期在开服日期之前,则往前推一个月卡周期
- }
- return startDate;
- }
- public static int CalculateDaysBetween(long startTimestamp, long endTimestamp)
- {
- // 将 Unix 时间戳转换为 DateTime
- DateTime startTime = DateTimeUtil.TimeStampToDateTime(startTimestamp);
- DateTime endTime = DateTimeUtil.TimeStampToDateTime(endTimestamp);
- // 计算调整后的 DateTime 对象之间的天数差异
- TimeSpan difference = endTime - startTime;
- int daysDifference = difference.Days;
- // 检查是否还有额外的时间差(小时、分钟、秒)
- if (difference.Hours > 0 || difference.Minutes > 0 || difference.Seconds > 0)
- {
- daysDifference++; // 如果有,天数差+1
- }
- return daysDifference;
- }
- private static DateTime AdjustDateTime(DateTime dateTime)
- {
- // 如果时间小于凌晨5点,则将日期减去一天
- if (dateTime.TimeOfDay < TimeSpan.FromHours(5))
- {
- dateTime = dateTime.AddDays(-1);
- }
- // 将时间调整为凌晨0点
- dateTime = dateTime.Date;
- return dateTime;
- }
- //按照凌晨5点为00点,获取当前月份
- public static int GetCurrentMonth()
- {
- DateTime currentDate = DateTime.Now;
- DateTime adjustedDate = currentDate.AddHours(-5); // 调整为当天上午5:00
- return adjustedDate.Month;
- }
- public static string GetSundayDateInt()
- {
- // 获取今天的日期
- DateTime today = DateTime.Today;
- // 计算今天到周日的天数差
- int daysUntilSunday = ((int)DayOfWeek.Sunday - (int)today.DayOfWeek + 7) % 7;
- // 获取这周的周日日期
- DateTime sunday = today.AddDays(daysUntilSunday);
- return sunday.ToString("yyyyMMdd");
- }
-
- public static int GetDateInt()
- {
- // 获取当前时间
- DateTime now = DateTime.Now;
- // 判断当前时间是否在凌晨5点之前
- if (now.Hour < 5)
- {
- // 如果在凌晨5点之前,返回昨天的日期
- DateTime yesterday = now.AddDays(-1);
- return Convert.ToInt32(yesterday.ToString("yyyyMMdd"));
- }
- else
- {
- // 否则返回今天的日期
- return Convert.ToInt32(now.ToString("yyyyMMdd"));
- }
- }
-
- /// <summary>
- /// 年月日 解析到某个时间点
- /// </summary>
- /// <param name="weekDate"></param>
- /// <param name="cfgClearingEndTime"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static DateTime GetDateTime(int weekDate, string cfgClearingEndTime)
- {
- // 将整数型日期转换为字符串形式
- string dateStr = weekDate.ToString();
- // 完整的日期时间字符串
- string dateTimeStr = dateStr + cfgClearingEndTime;
- // 解析成 DateTime 对象
- if (DateTime.TryParseExact(dateTimeStr.Trim(), "yyyyMMddHH:mm:ss", CultureInfo.InvariantCulture,
- DateTimeStyles.None, out var dateTime))
- {
- return dateTime;
- }
- throw new Exception("无法解析提供的日期时间字符串");
- }
- }
- }
|