Created
April 8, 2014 17:25
-
-
Save skipjac/10159241 to your computer and use it in GitHub Desktop.
Get the week number for a given 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
getWeekNumber(Date.now()) |
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) { | |
// Copy date so don't modify original | |
d = new Date(+d); | |
d.setHours(0,0,0); | |
// Set to nearest Thursday: current date + 4 - current day number | |
// Make Sunday's day number 7 | |
d.setDate(d.getDate() + 4 - (d.getDay()||7)); | |
// Get first day of year | |
var yearStart = new Date(d.getFullYear(),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.getFullYear(), weekNo]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment