Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain why is this in the jQuery UI Datepicker?

This method is on line 1380 in the jQuery ui datepicker:

_daylightSavingAdjust: function(date) {
    if (!date) {
      return null;
    }
    date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0);
    return date;
}

It is used in many places in the datepicker code. As far as I can see this code will just add 2 hours to any date that has the hours > 12 otherwise set the hours to 0.

I tried it in my console:

date = new Date();
console.log("" + date);
date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0);
console.log("" + date);

And I got:

Tue Dec 06 2011 18:36:06 GMT+0100 (CET)
Tue Dec 06 2011 20:36:06 GMT+0100 (CET)
like image 757
disc0dancer Avatar asked Dec 06 '11 17:12

disc0dancer


1 Answers

The comment is a bit cryptic indeed:

/* Handle switch to/from daylight saving.
   Hours may be non-zero on daylight saving cut-over:
   > 12 when midnight changeover, but then cannot generate
   midnight datetime, so jump to 1AM, otherwise reset.
   @param  date  (Date) the date to check
   @return  (Date) the corrected date */

The Datepicker creates date objects internally from the chosen dates without specifying the time - so midnight is used. In some locations, DST kicks in at midnight which throws the time back to 11PM the previous day.

So this function looks at the hour of the Date object to determine if it has to push the time forward by two hours to get the correct day.

like image 175
user1015989 Avatar answered Sep 22 '22 14:09

user1015989