Skip to content

Instantly share code, notes, and snippets.

@jazzcelaya
Created February 25, 2021 21:11
Show Gist options
  • Save jazzcelaya/88badab718d78a02a4241c9e708b6767 to your computer and use it in GitHub Desktop.
Save jazzcelaya/88badab718d78a02a4241c9e708b6767 to your computer and use it in GitHub Desktop.
Week number of the year with JS (Starting on Sundays)
Date.prototype.getWeekStartingOnSunday = function() {
var date = new Date(this.getTime());
// Thursday in current week decides the year.
// offset +1 because the week starts on Sunday
date.setDate(date.getDate() + 4 - (date.getDay()+1 + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 4 + (week1.getDay()+1 + 6) % 7) / 7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment