/// <summary>
/// 获取周
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
static int GetWeekNumber(int year)
{
DateTime now = Convert.ToDateTime(year + "-12-31");
var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(now, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return firstWeek;
}
/// <summary>
/// 周开始时间
/// </summary>
/// <param name="year"></param>
/// <param name="weekNumber"></param>
/// <returns></returns>
static DateTime GetFirstDateOfWeek(int year, int weekNumber)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
DateTime firstThursday = jan1.AddDays(daysOffset);
var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstThursday, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = weekNumber;
if (firstWeek <= 1)
{
weekNum -= 1;
}
DateTime result = firstThursday.AddDays(weekNum * 7);
return result.AddDays(-3);
}
/// <summary>
/// 周结束时间
/// </summary>
/// <param name="year"></param>
/// <param name="weekNumber"></param>
/// <returns></returns>
static DateTime GetLastDateOfWeek(int year, int weekNumber)
{
DateTime firstDayOfWeek = GetFirstDateOfWeek(year, weekNumber);
return firstDayOfWeek.AddDays(6);
}
标签:System,DayOfWeek,int,指定,DateTime,获取,Globalization,时间,year
From: https://www.cnblogs.com/sylAll/p/17519065.html