一年中的一周 C# 日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10102714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Week of Year C# Datetime
提问by Nicolas
I have following code to get the weeknumber of the year given in the DateTime object Time
我有以下代码来获取 DateTime 对象 Time 中给出的年份的周数
public static int WeeksInYear(DateTime date)
{
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
Now i give the function the date 1/1/2012 wich should return week 1 , but hes returning week 52. And i can't seem to figure out why. Anyone have an idea why ?
现在我给函数日期 1/1/2012 应该返回第 1 周,但他返回第 52 周。我似乎无法弄清楚为什么。任何人都知道为什么?
回答by KingCronus
The algorithm is doing exactly what you have instructed it to do. You have the CalanderWeekRule set to FirstFourDayWeek. The 1st of January 2012 was not part of the first four day week, so you have instructed the calander to start counting from January 2nd.
该算法完全按照您的指示执行操作。您已将 CalanderWeekRule 设置为 FirstFourDayWeek。2012 年 1 月 1 日不是前四天周的一部分,因此您已指示日历从 1 月 2 日开始计数。
回答by Nick
I assume that because 1/1/2012 was a Sunday and GetWeekOfYearsays it returns the week which includes the date, it's returning the last week of 2011 rather than the first week of 2012.
我假设因为 2012 年 1 月 1 日是星期日并GetWeekOfYear说它返回包含日期的那一周,所以它返回的是 2011 年的最后一周而不是 2012 年的第一周。
Have a look at the CalendarRulefor clarification.
查看CalendarRule以获得澄清。
回答by lkurylo
1/1/2012 it was sunday. I believe that's why you get 52, because it was last day of the last year week. For the 2nd of january you should get the right result.
2012 年 1 月 1 日是星期天。我相信这就是你得到 52 的原因,因为那是去年一周的最后一天。对于 1 月 2 日,您应该会得到正确的结果。
回答by basti
The weeknumber was correctly calculated. You should have a read on how weeknumbers are actually calculated/counted (Wikipedia?!).
周数计算正确。您应该阅读一下周数是如何实际计算/计数的(维基百科?!)。
Attention: The built-in calculation of week-number-calculation is buggy. Microsoft describes the problem in the KnowledgeBase-Article 200299. It has problems with ISO-8601.
注意:周数计算的内置计算有问题。Microsoft 在知识库文章 200299 中描述了该问题。它有 ISO-8601 的问题。
回答by basti
You can use the class Weekof the Time Period Library for .NETwhich supports supports the ISO 8601 week numbering:
您可以使用支持 ISO 8601 周编号的类Weekof the Time Period Library for .NET:
TimeCalendar calendar = new TimeCalendar(
new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601 } );
Week week = new Week( new DateTime( 2012, 01, 01 ), calendar );
Console.WriteLine( "week #: ", week.WeekOfYear );
回答by Qmash
public static int WeeksInYear(DateTime date)
{
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
return cal.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
i think if change CalendarWeekRule.FirstFourDayWeekto CalendarWeekRule.FirstDaythen this will work fine.
我认为如果将CalendarWeekRule.FirstFourDayWeek更改为CalendarWeekRule.FirstDay那么这将正常工作。
i change this then its working fine.
我改变这个然后它工作正常。

