计算从日期到另一个日期的周数

时间:2015-10-15 13:39:12

标签: c# date dayofweek

我必须根据日期计算周数。这可能很容易,我已经这样做了(感谢另一个SO帖子):

public static int GetCurrentWeekNumber(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }
    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

新请求是计算周数不是从星期一开始,而是从星期四到下一个星期三(包括在内)。任何提示?

1 个答案:

答案 0 :(得分:2)

CultureInfo.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Tuesday;

编辑:(感谢评论)

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.cultureinfo(v=vs.110).aspx

CultureInfo co = new new CultureInfo("en-US", true);
co.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Tuesday;