/// <summary>
/// 返回某个日期在这一年中的第几周
/// </summary>
/// <returns></returns>
public static int GetOnWeekOfYear(DateTime dateTime)
{
GregorianCalendar gc = new GregorianCalendar();
return gc.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
/// <summary>
/// 判断两个时间是否在同一周
/// </summary>
/// <param name="baseTime">对比源</param>
/// <param name="targetTime">拿来做对比的时间</param>
/// <returns></returns>
public static bool InSameWeek(DateTime baseTime, DateTime targetTime)
{
DayOfWeek dayOfWeek = baseTime.DayOfWeek;
int temp = (int)dayOfWeek;
if (dayOfWeek == DayOfWeek.Sunday)
{
temp = 7;
}
//这周第一天
DateTime firstday = baseTime.AddDays((int)DayOfWeek.Monday - temp);
//这周最后一天
DateTime lastday = baseTime.AddDays(7 - temp).AddHours(23).AddMinutes(59).AddSeconds(59);
if (DateTime.Compare(targetTime, firstday) >= 0 && DateTime.Compare(targetTime, lastday) <= 0)
{
return true;
}
return false;
}
unity C# 时间点在一年中的第几周,和判断两个时间点是否在同一周
最新推荐文章于 2024-03-23 12:01:58 发布