0

How to get week number in US format. I tried this code but it is not correct.

   Date.prototype.getWeekNumber = function (date) {
                        var d = new Date(date);
                        d.setHours(0, 0, 0);
                        d.setDate(d.getDate() + 4 - (d.getDay() || 7));
                        return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
                    };
rajmohan
  • 1,618
  • 1
  • 15
  • 36
  • 1
    What exactly is different in the US week number compared to the 'common' week number? This question seems similar: http://stackoverflow.com/questions/7765767/show-week-number-with-javascript – MeanGreen Jul 22 '14 at 11:36
  • Agree with @MeanGreen – Jay Jul 22 '14 at 11:43
  • 1
    http://www.onlineconversion.com/day_week_number.htm try this site and enter pls enter 09/12/2014. It will give 36 as US standrad week number and 37 as ISO – rajmohan Jul 22 '14 at 11:46

1 Answers1

0

It's a little trickier than that. The first week of the year starts with the first Thursday of the year. So really, you probably should count how many Thursdays have passed in that year for the given date.

Taken from Wikipedia: https://en.wikipedia.org/wiki/Week_number#Week_numbering

For example, week 1 of 2004 (2004W01) ran from Monday 29 December 2003 to Sunday, 4 January 2004, because its Thursday was 1 January 2004, whereas week 1 of 2005 (2005W01) ran from Monday 3 January 2005 to Sunday 9 January 2005, because its Thursday was 6 January 2005 and so the first Thursday of 2005. The highest week number in a year is either 52 or 53 (it was 53 in the year 2004).

Jay
  • 3,445
  • 2
  • 24
  • 29