I have this code:
// setting first day of year
DateTime jan1 = new DateTime(year, 1, 1);
// setting day offset from the cultureinfo FirstDayOfWeek
int daysOffset = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
// setting the first monday of the year
DateTime firstMonday = jan1.AddDays(daysOffset);
// no. On first week
int firstWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(jan1, CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,DayOfWeek.Monday);
//
if (firstWeek <= 1)
{
week -= 1;
}
return firstMonday.AddDays(week * 7);
The problem is when im testing it with dates like these
var a = TimeHelper.Week_GetStartAndEnd(2005, 29); // 11/7/2005 needs to be 18/7/2005
var b = TimeHelper.Week_GetStartAndEnd(2005, 28); // 4/7/2005 needs to be 11/7/2005
var c = TimeHelper.Week_GetStartAndEnd(2005, 32); // 1/8/2005 needs to be 8/1/2005
var d = TimeHelper.Week_GetStartAndEnd(2005, 52); // 19/12/2005 needs to be 26/12/2005
var e = TimeHelper.Week_GetStartAndEnd(2005, 38); // 12/9/2005 needs to be 19/9/2005
var f = TimeHelper.Week_GetStartAndEnd(2005, 41); // 3/10/2005 needs to be10/10/2005
var g = TimeHelper.Week_GetStartAndEnd(2005, 30); // 18/7/2005 needs to be 25/7/2005
var h = TimeHelper.Week_GetStartAndEnd(2000, 44); // 23/10/2000 needs to be 30/10/200
var j = TimeHelper.Week_GetStartAndEnd(1995, 41); // 9/10/2005 OK!
var r = TimeHelper.Week_GetStartAndEnd(2012, 42); // 15/10/2012 OK!
var t = TimeHelper.Week_GetStartAndEnd(2016, 21); // 16/5/2016 needs to be 23/5/2016
var u = TimeHelper.Week_GetStartAndEnd(2008, 13); // 24/3/2008 OK!
var aa = TimeHelper.Week_GetStartAndEnd(2012, 28); // 9/7/2012 OK!
var ab = TimeHelper.Week_GetStartAndEnd(2014, 37); // 8/9/2014 OK!
var ba = TimeHelper.Week_GetStartAndEnd(2018, 52); // 24/12/2018 OK!
Why are half of them correct and the other half a week too early?