JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

Displaying Author Local Time

4. Daylight Saving

Join the Discussion

Questions? Comments?

Istead of having to adjust manually on the dates that daylight saving starts and finishes, you can if you wish place code after the dst=0 line to set the daylight saving flag based on the dates that your area goes on and off of daylight savings time. If the dates change each year you may still need to update the script once a year but the date of the update is no longer as critical.

Here (in Sydney) we usually go onto daylight savings time at 2am on the last Sunday in October and back to standard time at 3am on the last Sunday in March. The following code will perform this switch at midnight visitor local time on those dates which is within a day of the correct time for the change. (To be more accurate than this would require about three times as much code which I don't think is worth it in this instance).

var gmt = new Date;
var lsm = new Date;
var lso = new Date;
lsm.setMonth(2); // March
lsm.setDate(31);
var day = lsm.getDay();// day of week of 31st
lsm.setDate(31-day); // last Sunday
lso.setMonth(9); // October
lso.setDate(31);
day = lso.getDay();
lso.setDate(31-day);
if (gmt < lsm || gmt >= lso) dst = 1;

You would of course need to change the code to determine the appropriate dates for your local circumstances. Note that the month numbers run 0 = January through 11 = December and not 1 through 12.

Introduction | Get the Script | Configure the Script | Daylight Saving

Note that the script and the descriptions of the functions that it contains are rewritten from the "Ask Felgall" website with the permission of the copyright owner.

Explore JavaScript

About.com Special Features

JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.