From e55ee9e6e9977df6be4d8b9503a5c9756bcbdbd3 Mon Sep 17 00:00:00 2001 From: Keith Wood Date: Wed, 1 Apr 2009 10:41:22 +0000 Subject: [PATCH] Datepicker: fixed #4285 - Week of the Year off by one during daylight saving time And simplified calculation --- ui/ui.datepicker.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/ui/ui.datepicker.js b/ui/ui.datepicker.js index bc5b5cc13..05418b498 100644 --- a/ui/ui.datepicker.js +++ b/ui/ui.datepicker.js @@ -821,20 +821,13 @@ $.extend(Datepicker.prototype, { @param date Date - the date to get the week for @return number - the number of the week within the year that contains this date */ iso8601Week: function(date) { - var checkDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()); - var firstMon = new Date(checkDate.getFullYear(), 1 - 1, 4); // First week always contains 4 Jan - var firstDay = firstMon.getDay() || 7; // Day of week: Mon = 1, ..., Sun = 7 - firstMon.setDate(firstMon.getDate() + 1 - firstDay); // Preceding Monday - if (firstDay < 4 && checkDate < firstMon) { // Adjust first three days in year if necessary - checkDate.setDate(checkDate.getDate() - 3); // Generate for previous year - return $.datepicker.iso8601Week(checkDate); - } 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 + var checkDate = new Date(date.getTime()); + // Find Thursday of this week starting on Monday + checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7)); + var time = checkDate.getTime(); + checkDate.setMonth(0); // Compare with Jan 1 + checkDate.setDate(1); + return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1; }, /* Parse a string value into a date object. -- 2.39.5