Skip to content

Instantly share code, notes, and snippets.

@dtomasi
Created August 15, 2016 13:42
Show Gist options
  • Save dtomasi/edc8d67a540ff71fb2d7339f23163596 to your computer and use it in GitHub Desktop.
Save dtomasi/edc8d67a540ff71fb2d7339f23163596 to your computer and use it in GitHub Desktop.
Get Week of Year in Javascript
Date.prototype.getWeek = function() {
var jan4th = new Date(this.getFullYear(),0,4);
return Math.ceil((((this - jan4th) / 86400000) + jan4th.getDay()+1)/7);
}
var now = new Date();
var weekNumber = now.getWeek();
console.log(weekNumber);
@Alevale
Copy link

Alevale commented May 23, 2018

Seems like it's not correct,

Sunday 3 January 2010 is written as "2009-W53-7" but with this it shows that the weeknumber is "1"
Monday 29 December 2008 is written "2009-W01-1" same here, the week should be 01 and it shows 53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment