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
  • 94,086
  • 102
  • 195
  • 339
Butters
  • 877
  • 5
  • 14
  • 25

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
  • 429,027
  • 67
  • 649
  • 891