Created
January 11, 2016 11:25
-
-
Save xonlly/fdfeffd0be26d069750a to your computer and use it in GitHub Desktop.
Get the week number by date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getWeekNumber(d) { | |
d = new Date(+d); | |
d.setHours(0,0,0); | |
d.setDate(d.getDate() + 4 - (d.getDay()||7)); | |
var yearStart = new Date(d.getFullYear(),0,1); | |
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); | |
return [d.getFullYear(), weekNo]; | |
} | |
// Exemple: | |
getWeekNumber(new Date('2006/11/06')) | |
// Return: | |
Array [ 2006, 45 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment