##// END OF EJS Templates
Calculate the correct week number for weeks starting at any date. #4857...
Eric Davis -
r3675:b2e903ff7c7e
parent child
Show More
@@ -1694,15 +1694,27 Date.prototype.getDayOfYear = function() {
1694 1694 return Math.floor(time / Date.DAY);
1695 1695 };
1696 1696
1697 /** Returns the number of the week in year, as defined in ISO 8601. */
1697 /** Returns the number of the week in year, as defined in ISO 8601.
1698 This function is only correct if `this` is the first day of the week. */
1698 1699 Date.prototype.getWeekNumber = function() {
1699 var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1700 var DoW = d.getDay();
1701 d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
1702 var ms = d.valueOf(); // GMT
1703 d.setMonth(0);
1704 d.setDate(4); // Thu in Week 1
1705 return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
1700 var d = new Date(this.getFullYear(), this.getMonth(), this.getDate());
1701 var days = 1000*60*60*24; // one day in milliseconds
1702
1703 // get the thursday of the current week
1704 var this_thursday = new Date(
1705 d.valueOf() // selected date
1706 - (d.getDay() % 7)*days // previous sunday
1707 + 4*days // + 4 days
1708 ).valueOf();
1709
1710 // the thursday in the first week of the year
1711 var first_thursday = new Date(
1712 new Date(this.getFullYear(), 0, 4).valueOf() // January 4 is in the first week by definition
1713 - (d.getDay() % 7)*days // previous sunday
1714 + 4*days // + 4 days
1715 ).valueOf();
1716
1717 return Math.round((this_thursday - first_thursday) / (7*days)) + 1;
1706 1718 };
1707 1719
1708 1720 /** Checks date and time equality */
General Comments 0
You need to be logged in to leave comments. Login now