0

I need to get the weeknumber of a date.

I know this is asked many times before but all the answers here are not usefull for me since they return the dotnet weeknumber or the iso 8601 weeknumber.

What I need is a bit more challenging.
Let me explain with sample data that will be the easiest :

input date    desired weeknumber
01-jan-2012          1
02-jan-2012          2
31-dec-2012         52
01-jan-2013          1
06-jan-2013          1
07-jan-2013          2
31-dec-2013         52
01-jan-2014          1
05-jan-2014          1
06-jan-2014          2

I hope this makes clear what I need.
Its like if you would write weeknumbers on a printed calendar but with the first/Last week splitted to 1/52 in stead of 53

Searching in google returns lots of answers like this one example of answer, but none of them I found returns these result so I am hoping someone can help me with this or point me in the right direction.

EDIT : another way to explain it is this :

look at it as you would say to another person, the last day of the year a person would say it is week 52 and the first day of the year a person would say week 1. A person would never say it is week 53.
For 02-jan-2012 a person would say we are in week 2 of this year, not in week 1

EDIT : I finally convinced my employer that this is not practical and even impossible. The answers and comments in this thread is what I used for the convincing so you did all help me well afterall

Community
  • 1
  • 1
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • 1
    Your question doesn't make sense. If you start new weeks on Mondays, and you define 01-Jan-2012 as week 1, so 02-Jan-2012 starts week 2, then 24-dec-2012 starts week 53 and 31-dec-2012 starts week 54, not week 52. – user5090812 Sep 30 '16 at 15:53
  • why would 31-dec-2012 start with week 54 then ? – GuidoG Sep 30 '16 at 15:54
  • @user5090812 I edited my question in an attempt to explain what I mean – GuidoG Sep 30 '16 at 16:01
  • 366/7=52.28, so there will always be a week 53, won't there? Even if it's just for 2 days. – Thomas Weller Sep 30 '16 at 16:02
  • Get out a 2012 calendar and count every Monday in it, starting with `2` for January 2, `3` for January 9, etc. You will count `54` for December 31. – user5090812 Sep 30 '16 at 16:25
  • What day do you expect to be the first day of the week? If the year starts on a different day should it be week 1? since the first day of the week can move and the length of a year is 52.143 or 52.386 weeks long there will always be at least a week 53 and possibly a week 54 depending on what is considered the first day of the week. (even if you shift the entire calendar so whatever day `1/1` is `12/31` will still be week 53. – Matthew Whited Sep 30 '16 at 16:38
  • That is unless the first week is considered week 0. – Matthew Whited Sep 30 '16 at 16:38
  • @user5090812 I actually counted the mondays on 2012 as you suggested, there not 54 but 53 for december 31 – GuidoG Sep 30 '16 at 18:42
  • I fully agree that these are not correct and logic weeknumbers but I am asked to do it like this it is not my choice either – GuidoG Sep 30 '16 at 18:51
  • @GuidoG But you asked for the first Monday to be numbered as 2, because the first Sunday was numbered as 1. Please get your own requirements straight. – user5090812 Sep 30 '16 at 19:46
  • @user5090812 I never asked for the first monday to be numbered as 2. I still is the first monday, but it happens to be in week 2 because there are other days earlier in that year. That is not the same. – GuidoG Oct 01 '16 at 07:52
  • @user5090812 Check my latest update in the question. Thanks to this thread I convinced my employer to give up on this idea – GuidoG Oct 01 '16 at 15:54
  • @MatthewWhited Check my latest update in the question. Thanks to this thread I convinced my employer to give up on this idea – GuidoG Oct 01 '16 at 15:54

2 Answers2

0

I'm not sure if I understand your post correctly but maybe this could be a solution:

  var dates = new[]
  {
    new DateTime(2012, 1, 1),
    new DateTime(2012, 1, 2),
    new DateTime(2012, 12, 31),
    new DateTime(2013, 1, 1),
    new DateTime(2013, 1, 6),
    new DateTime(2013, 1, 7),
    new DateTime(2013, 12, 31),
    new DateTime(2014, 1, 1),
    new DateTime(2014, 1, 5),
    new DateTime(2014, 1, 6),
  };

  var calendar = CultureInfo.CurrentCulture.Calendar;

  foreach (var date in dates)
  {
    var day = calendar.GetDayOfYear(date);
    var week = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
    week = week > 52 ? 52 : week;
    Console.WriteLine($"{date.Date}: {day,3}: {week,3}");
  }
0

have you tried any codes? and what you say is not making sense , although weeks start on monday , a january 2nd on tuesday does not mean it's the years second week, week number in a year means that you want to know how many weeks have passed, meaning you have to devide the day number by 7. what i mean is that the week number is not dependant on the start of the weekdays , it depends on how many 7-days have passed. for this you have two ways to implement: 1st - first extract the day number from the date and then devide it to 7 to get the week number. 2nd - to create a switch case , and deviding the weeks by dates. although in single cases the 2nd way might prove faster, the 1st ways proves to be easier to make it into code, needless to say both are a little too long, if you don't understand what to do or how to do it comment so i put first few lines of each one's codes.

Hitman2847
  • 117
  • 6
  • I know that 2 january 2012 (a monday) is not considered the second week but the first week. The thing is I have to make this like descibed and I have found many answers here on SO on how to get weeknumbers from a date but never like this. It is not my choice either – GuidoG Sep 30 '16 at 18:48
  • so you do one of those methods , just add a control method to check what daybit is and if needed change those answers, my choice is to use switch case eather than multiple ifs – Hitman2847 Sep 30 '16 at 19:47
  • Check my latest update in the question. Thanks to this thread I convinced my employer to give up on this idea – GuidoG Oct 01 '16 at 15:57
  • i suggest you delete the question yourself , closing it by another user might not have negative rep but it will be listed as closed on your asked list – Hitman2847 Oct 01 '16 at 16:01