我如何计算/找到一个给定日期的周数? [英] How can I calculate/find the week-number of a given date?

查看:410
本文介绍了我如何计算/找到一个给定日期的周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何计算/找到一个给定日期的周数?


解决方案

  VAR的CurrentCulture = CultureInfo.CurrentCulture;
VAR weekNo = currentCulture.Calendar.GetWeekOfYear(
                新的日期时间(2013,12,31)
                currentCulture.DateTimeFormat.CalendarWeekRule,
                currentCulture.DateTimeFormat.FirstDayOfWeek);

请注意,这是不会 ISO 8601 兼容。在瑞典,我们使用ISO 8601 电话号码,但尽管文化被设置为SV-SE, CalendarWeekRule FirstFourDayWeek Firstdayofweek可是周一的 weekNo 的变量将被设置为 53 而不是正确的 1 在上面的$ C $角

我只试图与瑞典的设置,但我是pretty确保在使用ISO 8601的星期数所有国家(奥地利,德国,瑞士等)会受此问题影响。

彼得·Ooijen 和<一个href=\"http://blogs.msdn.com/shawnste/archive/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net.aspx\"相对=nofollow>肖恩·斯蒂尔有不同的解决这个问题。

下面是一个紧凑的解决方案。

 私有静态诠释WeekOfYearISO8601(DateTime的日期)
{
    VAR天=(int)的CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(日期);
    返回CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (天== 0 7:?天)),CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
}

这是一个为以下日期进行了测试。

  VAR datesAndISO8601Weeks =新词典&LT;日期时间,INT&GT;
                        {
                            {新的DateTime(2000,12,31),52},
                            {新的DateTime(2001,1,1),1},
                            {新的DateTime(2005,1,1),53},
                            {新的DateTime(2007,12,31),1},
                            {新的DateTime(2008,12,29),1},
                            {新日期时间(2010年1,3),53},
                            {新的DateTime(2011,12,31),52},
                            {新的DateTime(2012,1,1),52},
                            {新日期时间(2013年1,2),1},
                            {新的DateTime(2013,12,31),1},
                        };的foreach(在datesAndISO8601Weeks VAR dateWeek)
{
    Debug.Assert的(WeekOfYearISO8601(dateWeek.Key)== dateWeek.Value,dateWeek.Key.ToShortDateString()+应为周数+ dateWeek.Value +,但被+ WeekOfYearISO8601(dateWeek.Key));
}

How can I calculate/find the week-number of a given date?

解决方案

var currentCulture = CultureInfo.CurrentCulture;
var weekNo = currentCulture.Calendar.GetWeekOfYear(
                new DateTime(2013, 12, 31),
                currentCulture.DateTimeFormat.CalendarWeekRule,
                currentCulture.DateTimeFormat.FirstDayOfWeek);

Be aware that this is not ISO 8601 compatible. In Sweden we use ISO 8601 week numbers but even though the culture is set to "sv-SE", CalendarWeekRule is FirstFourDayWeek, and FirstDayOfWeek is Monday the weekNo variable will be set to 53 instead of the correct 1 in the above code.

I have only tried this with Swedish settings but I'm pretty sure that all countries (Austria, Germany, Switzerland and more) using ISO 8601 week numbers will be affected by this problem.

Peter van Ooijen and Shawn Steele has different solutions to this problem.

Here's a compact solution

private static int WeekOfYearISO8601(DateTime date)
{
    var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
    return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

It's been tested for the following dates

var datesAndISO8601Weeks = new Dictionary<DateTime, int>
                        {
                            {new DateTime(2000, 12, 31), 52},
                            {new DateTime(2001, 1, 1), 1},
                            {new DateTime(2005, 1, 1), 53},
                            {new DateTime(2007, 12, 31), 1},
                            {new DateTime(2008, 12, 29), 1},
                            {new DateTime(2010, 1, 3), 53},
                            {new DateTime(2011, 12, 31), 52},
                            {new DateTime(2012, 1, 1), 52},
                            {new DateTime(2013, 1, 2), 1},
                            {new DateTime(2013, 12, 31), 1},
                        };

foreach (var dateWeek in datesAndISO8601Weeks)
{
    Debug.Assert(WeekOfYearISO8601(dateWeek.Key) == dateWeek.Value, dateWeek.Key.ToShortDateString() + " should be week number " + dateWeek.Value + " but was " + WeekOfYearISO8601(dateWeek.Key));
}

这篇关于我如何计算/找到一个给定日期的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆