0

I need to display the the current time in the Central Timzone, reguardless of the client's local timezone, and to do that I need to know if the Central timezone is at UTC-6 or UTC-5

What is the best way to handle daylight savings in Javascript?

ContextSwitch
  • 2,830
  • 6
  • 35
  • 51
  • http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – aarryy Oct 09 '13 at 17:50

3 Answers3

1

First, you would need a JavaScript library that implements the IANA time zone database. I list several of them here.

The IANA zone name for US Central Time is America/Chicago.

If all you want to know is the current time in that zone, with DST applied appropriately, then all of those libraries have some form of that as their primary use case.

Probably the easiest example is with moment-timezone, where you can simply do this:

var nowInCentralTime = moment().tz("America/Chicago");
var stringToOuput = nowInCentralTime.format("LLL");

(Of course, you can apply any format you want. See the moment.js docs for details).

And if you want to know if this is DST or not, you can do this:

var centralIsInDST = nowInCentralTime.isDST();
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

I ended up doing this on the server and passing it to the client after checking this post: Daylight saving time and time zone best practices

Never trust client datetime. It may very well be incorrect.
Community
  • 1
  • 1
ContextSwitch
  • 2,830
  • 6
  • 35
  • 51
0

Find the offset when it's defiantly not DST in their local area

var zone,
    z1 = new Date(2013, 0, 1).getTimezoneOffset(),
    z2 = new Date(2013, 5, 1).getTimezoneOffset();
if (z2 < z1) // northern hemisphere
    zone = z1;
else         // southern hemisphere
    zone = z2;
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • But is there a way to know if the central timezone is in DST? – ContextSwitch Oct 09 '13 at 17:56
  • Oh; _JavaScript_ doesn't expose anything other than local timezone and _UTC_, I thought you were asking about always showing the local time in non-DST, not some third timezone. You'd need to know how to calculate the switches based upon _UTC_. – Paul S. Oct 09 '13 at 17:58