Last active
March 23, 2017 16:08
-
-
Save aindong/ec265f93496e43ef03a6 to your computer and use it in GitHub Desktop.
Get the week number of a selected 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
// Hacking javascript to get the week number of a selected date | |
Date.prototype.getWeek = function() { | |
var date = new Date(this.getTime()); | |
date.setHours(0, 0, 0, 0); | |
// Thursday in current week decides the year. | |
date.setDate(date.getDate() + 3 - (date.getDay() + 7) % 7); | |
// January 4 is always in week 1. | |
var week1 = new Date(date.getFullYear(), 0, 4); | |
// Adjust to Thursday in week 1 and count number of weeks from date to week1. | |
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 | |
- 3 + (week1.getDay() + 6) % 7) / 7); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment