0

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?

cspolton
  • 4,495
  • 4
  • 26
  • 34
  • 1
    So what you're trying to do is get the date of the first day of the week number you specify? If so, probably a duplicate of http://stackoverflow.com/questions/662379/calculate-date-from-week-number – Serdalis Sep 02 '12 at 09:15
  • 2
    Must be a CultureInfo problem, partially : tested, and few of your NOK are OK to me, and few OK are NOK (I'm in fr-CH) – Raphaël Althaus Sep 02 '12 at 09:32
  • If you are open to alternatives, consider using [NodaTime](http://code.google.com/p/noda-time/). There is a SO question covering just this for `NodaTime`: http://stackoverflow.com/q/11342856/250725 – psubsee2003 Sep 02 '12 at 09:33
  • You are using CurrentCulture, determine if that defines the right startday (Monday | Sunday). You can always hard-code it. – H H Sep 02 '12 at 09:42
  • 1
    Finally, I got it work, taking the code in the same post as you... but another answer http://stackoverflow.com/a/9064954/961526 (Mikael Svenson's one) – Raphaël Althaus Sep 02 '12 at 09:51
  • 1
    Yes, Mikael has a good ISO8601 answer. It all depends on the rules you need to follow, cultures and businesses have come up with many. – H H Sep 02 '12 at 10:44
  • Hey guys ! thanks to all of you for the really fast answer, and yea serdalist was a duplicate post(to stupid to type europe into search when i looked) but it works now with mikael svensons answer! HAVE A NICE SUNDAY! – Mrlondon7100 Sep 02 '12 at 13:44

0 Answers0