WeekNumber计算中的错误.NET如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (24)

我有一个相当奇怪的问题。2013年的第一周(第一周)从2012年12月31日开始,持续7天 - 正常情况下通常是

根据.NET,12月30日是第52周,第31周是第53周,1月1日是第1周。

第53周仅持续一天,第1周持续6天。显然这肯定是错误的(一周不到7天),在丹麦语中肯定是错误的。十二月三十一日是第一周,而非第五十三天。

以下代码说明了这个问题(CurrentCulture是“da-DK”)

    static void Main(string[] args)
    {
        //Here I get Monday
        DayOfWeek firstDayOfWeek = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;             
        //Here I get FirstFourDayWeek
        CalendarWeekRule weekRule = DateTimeFormatInfo.CurrentInfo.CalendarWeekRule; 

        DateTime date = new DateTime(2012,12,30);

        for (int i = 0; i <= 10; i++)
        {
            DateTime currentDate = date.AddDays(i);
            Console.WriteLine("Date: {0} WeekNumber: {1}",
                currentDate.ToShortDateString(),
                CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(currentDate, weekRule, firstDayOfWeek));
        }
        Console.ReadLine();
    }

我做错了什么或者这是一个在.NET中的错误?如果后者 - 你有没有正确计算周数的建议?

提问于
用户回答回答于

问题在于该GetWeekOfYear方法不尊重ISO 8601,这正是所期望的,但事实并非如此。

请注意,在使用时FirstFourDayWeek文档说:

基于FirstFourDayWeek价值的第一周可能需要四到七天。

这违反了ISO 8601规则,即所有星期必须有七天

也:

根据ISO 8601,可以使用以下方法获取正确的周数:

int weekNumber(DateTime fromDate)
{
    // Get jan 1st of the year
    DateTime startOfYear = fromDate.AddDays(- fromDate.Day + 1).AddMonths(- fromDate.Month +1);
    // Get dec 31st of the year
    DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
    // ISO 8601 weeks start with Monday 
    // The first week of a year includes the first Thursday 
    // DayOfWeek returns 0 for sunday up to 6 for saterday
    int[] iso8601Correction = {6,7,8,9,10,4,5};
    int nds = fromDate.Subtract(startOfYear).Days  + iso8601Correction[(int)startOfYear.DayOfWeek];
    int wk = nds / 7;
    switch(wk)
    {
        case 0 : 
            // Return weeknumber of dec 31st of the previous year
            return weekNumber(startOfYear.AddDays(-1));
        case 53 : 
            // If dec 31st falls before thursday it is week 01 of next year
            if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
                return 1;
            else
                return wk;
        default : return wk;
    }
}

来源(这里还有很多其他功能......)

所以,改变你的循环

for (int i = 0; i <= 10; i++)
{
    DateTime currentDate = date.AddDays(i);
    Console.WriteLine("Date: {0} WeekNumber: {1}: CorrectWeekNumber: {2}",
        currentDate.ToShortDateString(),
        CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(currentDate, weekRule, firstDayOfWeek),
        weekNumber(currentDate));
}

将导致:

Date: 30.12.2012 WeekNumber: 52: CorrectWeekNumber: 52 Date: 31.12.2012 WeekNumber: 53: CorrectWeekNumber: 1 Date: 01.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 02.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 03.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 04.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 05.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 06.01.2013 WeekNumber: 1: CorrectWeekNumber: 1 Date: 07.01.2013 WeekNumber: 2: CorrectWeekNumber: 2 Date: 08.01.2013 WeekNumber: 2: CorrectWeekNumber: 2 Date: 09.01.2013 WeekNumber: 2: CorrectWeekNumber: 2

用户回答回答于

我最终创建了两个C#方法来实现我想要的:

首先在下面的一个评论中找到一个简明的评论:http : //blogs.msdn.com/b/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft- net.aspx

Jon Senchyna也指出:

     public static int WeekNumber(this DateTime date)
    {
        Calendar cal = CultureInfo.InvariantCulture.Calendar;
        DayOfWeek day = cal.GetDayOfWeek(date);
        date = date.AddDays(4 - ((int)day == 0 ? 7 : (int)day));
        return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    }

还有一个:http : //www.tondering.dk/claus/cal/week.php#calcweekno

    public static int WeekNumber2(this DateTime date)
    {
        int a;
        int b;
        int c;
        int s;
        int e;
        int f;

        if (date.Month <= 2)
        {
            a = date.Year - 1;
            b = a / 4 - a / 100 + a / 400;
            c = (a - 1) / 4 - (a - 1) / 100 + (a - 1) / 400;
            s = b - c;
            e = 0;
            f = date.Day - 1 + 31 * (date.Month - 1);
        }
        else
        {
            a = date.Year;
            b = a / 4 - a / 100 + a / 400;
            c = (a - 1) / 4 - (a - 1) / 100 + (a - 1) / 400;
            s = b - c;
            e = s + 1;
            f = date.Day + ((153 * (date.Month - 3) + 2) / 5) + 58 + s;
        }

        int g = (a + b) % 7;
        int d = (f + g - e) % 7;
        int n = f + 3 - d;

        if (n < 0)
            return 53 - ((g - s) / 5);
        if (n > (364 + s))
            return 1;
        return n / 7 + 1;
    }

两人都给了我想要的东西。

我还写了一个小单元测试,证明他们在日历的前3000年返回相同的周数。

    [TestMethod]
    public void WeekNumbers_CorrectFor_3000Years()
    {
        var weekNumbersMethod1 = WeekNumbers3000Years(DateManipulation.WeekNumber).ToList();
        var weekNumbersMethod2 = WeekNumbers3000Years(DateManipulation.WeekNumber2).ToList();
        CollectionAssert.AreEqual(weekNumbersMethod1, weekNumbersMethod2);
    }

    private IEnumerable<int> WeekNumbers3000Years(Func<DateTime, int> weekNumberCalculator)
    {
        var startDate = new DateTime(1,1,1);
        var endDate = new DateTime(3000, 12, 31);
        for(DateTime date = startDate; date < endDate; date = date.AddDays(1))
            yield return weekNumberCalculator(date);
    }

扫码关注云+社区