Skip to content

Instantly share code, notes, and snippets.

@xonlly
Created January 11, 2016 11:25
Show Gist options
  • Save xonlly/fdfeffd0be26d069750a to your computer and use it in GitHub Desktop.
Save xonlly/fdfeffd0be26d069750a to your computer and use it in GitHub Desktop.
Get the week number by date.
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