本文介绍了System.Globalization.Calendar.GetWeekOfYear()返回奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算周数的日期的中间,但是 System.Globalization.Calendar 正在返回奇怪的结果(其中包括年)十二月年31日2007年和2012年。

I'm in the middle of calculating week numbers for dates, but the System.Globalization.Calendar is returning odd results for (amongst other years) December 31st of year 2007 and 2012.

Calendar calendar = CultureInfo.InvariantCulture.Calendar;
var date = new DateTime(2007, 12, 29);
for (int i = 0; i < 5; i++)
{
    int w = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    Console.WriteLine("{0}\t{1}", date.ToString("dd.MM.yyyy"), w);
    date = date.AddDays(1);
}

结果

29.12.2007      52
30.12.2007      52
31.12.2007      53 <--
01.01.2008       1
02.01.2008       1

29.12.2012      52
30.12.2012      52
31.12.2012      53 <--
01.01.2013       1
02.01.2013       1

据我了解,应该不会每周53是2007年度和2012年,但日内应包括在1周有没有办法改变的日历这种行为

推荐答案

的documentation为CalendarWeekRule枚举明确指出,它并不直接映射到ISO 8601,并链接到ISO 8601本周在微软的.Net ,描述不同博客条目年格式。

The documentation for the CalendarWeekRule enumeration specifically states that it "does not map directly to ISO 8601", and links to ISO 8601 Week of Year format in Microsoft .Net, a blog entry that describes the differences.

这篇关于System.Globalization.Calendar.GetWeekOfYear()返回奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 08:59