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.
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.
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.