Created
January 14, 2023 19:16
-
-
Save MegaMinxCoding/08f1a29930c3966b7db1d2375a4de25c to your computer and use it in GitHub Desktop.
Output the calendar week of a date
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
| function calendarWeek(d) { | |
| // Copy date so don't modify original | |
| d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); | |
| // Set to nearest Thursday: current date + 4 - current day number | |
| // Make Sunday's day number 7 | |
| d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); | |
| // Get first day of year | |
| var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); | |
| // Calculate full weeks to nearest Thursday | |
| var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); | |
| // Return array of year and week number | |
| return [d.getUTCFullYear(), weekNo]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment