0

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Butters
  • 977
  • 5
  • 14
  • 25
  • 1
    .NET week handling is broken. You should check out [Noda-Time](http://nodatime.org/). – Lasse V. Karlsen Nov 14 '13 at 09:08
  • possible duplicate of [Get the correct week number of a given date](http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date) – H H Nov 14 '13 at 09:44
  • "Correct" is not applicable. You should know which rule to follow, unfortubnaltely that depends on (business) culture. – H H Nov 14 '13 at 09:45

1 Answers1

2

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 ]

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939