2

How can I calculate the date in C# when I recieve the year, weeknumber and day in week. For example: Year = 2011 Week = 27 day = 6

result should be 2011-7-10


Thanks to all. I solved it based on the wikipedia algorithm.

Dori
  • 915
  • 1
  • 12
  • 20
user840828
  • 21
  • 1

2 Answers2

2

No C# code here (sorry), but assuming that you're talking about ISO week date, you can find a general algorithm that calculates the Gregorian date of an ISO week date in Wikipedia. Hope it helps.

kodkod
  • 1,556
  • 4
  • 21
  • 43
2

This should work:

 public static DateTime GetDateTime(int year, int week, int day, CultureInfo cultureInfo)
    {
        DateTime firstDayOfYear = new DateTime(year, 1, 1);
        int firstWeek = cultureInfo.Calendar.GetWeekOfYear(firstDayOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
        int dayOffSet = day - (int)cultureInfo.DateTimeFormat.FirstDayOfWeek + 1;
        return firstDayOfYear.AddDays((week - (firstWeek + 1)) * 7 + dayOffSet + 1);
    }

I should point out my implementation is not zero based. (so Year=2011, Week=27, day=6) is july 2nd, 2011.

InBetween
  • 32,319
  • 3
  • 50
  • 90