0

got this get week script

    var d = new Date("2015-10-19T18:29:27.359Z");
    d.setHours(0,0,0);
    d.setDate(d.getDate()+4-(d.getDay()||7));
   var week = Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7))/7);
console.log(week);

should return 43 (week number of 19/10) but it returns 42

var d = new Date("2015-10-26T18:29:27.359Z");

returns week 44 as it should

looks likes it jumps over week 43?

Thomas
  • 113
  • 1
  • 1
  • 12
  • 1
    Possible duplicate of [Get week of year in JavaScript like in PHP](http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php) – kemicofa ghost Nov 01 '15 at 18:39
  • 2
    When you say *"got this week script"*, does that mean you wrote it? Or does it mean you found it somewhere on the internet and now want us to debug it for you? –  Nov 01 '15 at 18:42
  • This script is not reliable because it doesn't take into account TimeZones and changes in DST. – derpirscher May 27 '22 at 16:08

2 Answers2

0

If you check this reference:

http://www.w3schools.com/jsref/jsref_obj_date.asp

You will see it says week is zero-based

juan garcia
  • 1,096
  • 1
  • 14
  • 49
0

Instead of calculating it manually: use week input

var elm = document.createElement('input')
elm.type = 'week'
elm.valueAsDate = new Date('2015-10-19T18:29:27.359Z')
var week = elm.value.slice(6)

console.log(week)
Endless
  • 29,359
  • 11
  • 97
  • 120