Created
February 25, 2021 21:11
-
-
Save jazzcelaya/88badab718d78a02a4241c9e708b6767 to your computer and use it in GitHub Desktop.
Week number of the year with JS (Starting on Sundays)
This file contains hidden or 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
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