-1

All:

I wonder if there is a function in javascript that can get the week order of a Date, for example:

01/05/2016 is in the second week of this year, so the week order is 1(let start by 0)

Thanks

Kuan
  • 11,149
  • 23
  • 93
  • 201
  • Your definition of "week" is different to ISO, which defines the start of a week as Monday and the week number is based on the Thursday, so 5 January is in the first week, not the second. Also consider [*Get week of year in JavaScript like in PHP*](http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php/6117889#6117889). – RobG Jun 09 '16 at 21:10

1 Answers1

0

You can calculate the days pass from the begining of the year and calculate the number of weeks by deviding it to 7 (yhe number of the days in a week):

var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var week = Math.floor(day/7);

alert(week);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Code–only answers aren't helpful. What day is the start of a week? How is the "first" week determined? The *start* variable will be 31 December of the previous year, is that intended? – RobG Jun 09 '16 at 21:17
  • @RobG I added a description – Ashkan Mobayen Khiabani Jun 09 '16 at 21:19
  • Cool, that returns 0 for 2016-01-05, which is in week 1 based on ISO and the OP's requirements. The week number of a year is not typically based on the number of 7 days periods that have passed, there are different schemes based on [*different algorithms*](https://en.wikipedia.org/wiki/Week#Week_numbering). – RobG Jun 09 '16 at 22:57