]> source.dussan.org Git - jquery-ui.git/commitdiff
Datepicker: fixed #4285 - Week of the Year off by one during daylight saving time
authorKeith Wood <kbwood.au@gmail.com>
Wed, 1 Apr 2009 10:41:22 +0000 (10:41 +0000)
committerKeith Wood <kbwood.au@gmail.com>
Wed, 1 Apr 2009 10:41:22 +0000 (10:41 +0000)
And simplified calculation

ui/ui.datepicker.js

index bc5b5cc1323d45177cb5084dca73e8c3add3fd65..05418b498df286935c737ec3392ff8c5bb8bc100 100644 (file)
@@ -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.