获取给定日期的正确的周数

获取给定日期的正确的周数

本文介绍了获取给定日期的正确的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索了很多,发现了很多解决方案,但他们都不给我了2012-12-31正确的周数。即使MSDN(link)失败。

2012-12-31就是星期一,所以它应该是第1周,但是每一个方法我试过给我53.下面是一些方法,我已经尝试过:

从MDSN库:

 的DateTimeFormatInfo DFI = DateTimeFormatInfo.CurrentInfo;
日历CAL = dfi.Calendar;

返回cal.GetWeekOfYear(日期,dfi.CalendarWeekRule,dfi.FirstDayOfWeek);
 

解决方案2:

 返回新的GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(日期,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
 

解决方案3:

 的CultureInfo ciCurr = CultureInfo.CurrentCulture;
INT WEEKNUM = ciCurr.Calendar.GetWeekOfYear(dtPassed,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
返回WEEKNUM;
 

更新

下面的方法实际上返回1时的日期是2012-12-31。换句话说,我的问题是,我的方法不符合ISO-8601标准。

  //这presumes这周开始的星期一。
//第1周是一年一个周四在它的第一个星期。
公共静态INT GetIso8601WeekOfYear(DateTime的时间)
{
    //严重作弊。如果周一,周二或周三,那么它会
    //是同一周#作为任何周四,周五或周六是,
    //我们总是让那些权
    DAYOFWEEK天= CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(时间);
    如果(天> = DayOfWeek.Monday和放大器;和天< = DayOfWeek.Wednesday)
    {
        时间= time.AddDays(3);
    }

    //返回我们调整星期
    返回CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(时间,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
}
 

解决方案

正如在此的有ISO8601周和.Net周编号之间的细微差别。

您可以参考这篇文章在MSDN博客的一个更好的解释:ISO 8601周的年份格式,微软的.Net

简单地说,净让周以跨年分裂,而ISO标准没有。在文章中也有一个简单的函数来得到正确的ISO 8601的周数为今年的最后一个星期。

更新下面的方法实际上返回1 2012-12-31 这是正确的ISO 8601(如德国)。

  //这presumes这周开始的星期一。
//第1周是一年一个周四在它的第一个星期。
公共静态INT GetIso8601WeekOfYear(DateTime的时间)
{
    //严重作弊。如果周一,周二或周三,那么它会
    //是同一周#作为任何周四,周五或周六是,
    //我们总是让那些权
    DAYOFWEEK天= CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(时间);
    如果(天> = DayOfWeek.Monday和放大器;和天< = DayOfWeek.Wednesday)
    {
        时间= time.AddDays(3);
    }

    //返回我们调整星期
    返回CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(时间,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
}
 

I have Googled a lot and found a lot of solutions, but none of them give me the correct week number for the 2012-12-31. Even the example on MSDN (link) fails.

2012-12-31 is Monday, therefore it should be Week 1, but every method I tried gives me 53. Here are some of the methods, that I have tried:

From the MDSN Library:

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);

Solution 2:

return new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

Solution 3:

CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;

Update

The following method actually returns 1 when date is 2012-12-31. In other words, my problem was that my methods were not following the ISO-8601 standard.

// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
    // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll
    // be the same week# as whatever Thursday, Friday or Saturday are,
    // and we always get those right
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    // Return the week of our adjusted day
    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
解决方案

As noted in this MSDN page there is a slight difference between ISO8601 week and .Net week numbering.

You can refer to this article in MSDN Blog for a better explanation: "ISO 8601 Week of Year format in Microsoft .Net"

Simply put, .Net allow weeks to be split across years while the ISO standard does not.In the article there is also a simple function to get the correct ISO 8601 week number for the last week of the year.

Update The following method actually returns 1 for 2012-12-31 which is correct in ISO 8601 (e.g. Germany).

// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
    // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll
    // be the same week# as whatever Thursday, Friday or Saturday are,
    // and we always get those right
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    // Return the week of our adjusted day
    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

这篇关于获取给定日期的正确的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:16