Skip to content

Commit

Permalink
Datepicker: fixed #4285 - Week of the Year off by one during daylight…
Browse files Browse the repository at this point in the history
… saving time

And simplified calculation
  • Loading branch information
Keith Wood committed Apr 1, 2009
1 parent 1262100 commit e55ee9e
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions ui/ui.datepicker.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -821,20 +821,13 @@ $.extend(Datepicker.prototype, {
@param date Date - the date to get the week for @param date Date - the date to get the week for
@return number - the number of the week within the year that contains this date */ @return number - the number of the week within the year that contains this date */
iso8601Week: function(date) { iso8601Week: function(date) {
var checkDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()); var checkDate = new Date(date.getTime());
var firstMon = new Date(checkDate.getFullYear(), 1 - 1, 4); // First week always contains 4 Jan // Find Thursday of this week starting on Monday
var firstDay = firstMon.getDay() || 7; // Day of week: Mon = 1, ..., Sun = 7 checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
firstMon.setDate(firstMon.getDate() + 1 - firstDay); // Preceding Monday var time = checkDate.getTime();
if (firstDay < 4 && checkDate < firstMon) { // Adjust first three days in year if necessary checkDate.setMonth(0); // Compare with Jan 1
checkDate.setDate(checkDate.getDate() - 3); // Generate for previous year checkDate.setDate(1);
return $.datepicker.iso8601Week(checkDate); return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
} else if (checkDate > new Date(checkDate.getFullYear(), 12 - 1, 28)) { // Check last three days in year
firstDay = new Date(checkDate.getFullYear() + 1, 1 - 1, 4).getDay() || 7;
if (firstDay > 4 && (checkDate.getDay() || 7) < firstDay - 3) { // Adjust if necessary
return 1;
}
}
return Math.floor(((checkDate - firstMon) / 86400000) / 7) + 1; // Weeks to given date
}, },


/* Parse a string value into a date object. /* Parse a string value into a date object.
Expand Down

0 comments on commit e55ee9e

Please sign in to comment.