Question

I need an right calendarweek for my application. This is my try:

DateTime d = new DateTime(2013,12,31);
CultureInfo CUI = CultureInfo.CurrentCulture;
Label1.Text =  CUI.Calendar.GetWeekOfYear(d, CUI.DateTimeFormat.CalendarWeekRule, CUI.DateTimeFormat.FirstDayOfWeek).ToString();

And now I get the 53 but this is wrong for this year. Correctly it should be the first Calendar week.

And December 2015 on 31-12-2013 the we have the next time the 53 calendarweek.

Was it helpful?

Solution

You can use this method which returns 1:

public static int GetIso8601WeekOfYear(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

[ borrowed from Get the correct week number of a given date ]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top