Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date - how to know whether DST is in effect in a given timezone

First off, I am NOT looking for whether DST is in effect locally.

I'm running a Node process that has data that has associated timestamps. I need to convert those timestamps to a day/month/year in a specified time zone, but all I'm given is the time zone's offset and DST offset.

I wish Date / Moment worked better with time zones. They work great with UTC or Local time zones, but it seems you need to hack it to get something else.

Is there something I'm missing? Assuming I can determine whether DST is in effect, would this work:

var d = new Date(ts + timezone_offset - local_offset);
d.getMonth();
d.getDate();

where timezone_offset is the time zone's offset (either the standard offset or the dst one)?

How might I determine whether DST is in effect?

like image 619
user1756980 Avatar asked Feb 08 '13 19:02

user1756980


People also ask

Is date now affected by timezone?

The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a Number. My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset.

Does JavaScript date handle daylight Savings?

The answer is Yes. If we are considering New York, USA, then during Daylight Saving, the offset is -4, whereas it would otherwise be -5. There's a solution for this as well: an additional function while obtaining destination city's UTC offset.

How does JavaScript detect timezone?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

Is UTC affected by daylight savings?

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.


1 Answers

First, recognize that if all you have are the offsets, you cannot solve this problem. You must have a time zone identifier, such as "America/New_York". Since in the question comments you said you do indeed have this, then you can use one of these libraries to get the job done.

I had previously posted another answer to this question, recommending TimeZoneJS, but I'll retract that now - as this question is specifically about DST, and TimeZoneJS has bugs, reporting values incorrectly near DST transitions.

Instead, I'll now recommend using moment.js with the moment-timezone add-on. Once installing both libraries (being sure to load actual time zone data, per the docs), the code is quite simple:

moment(timestamp).tz(timezone).isDST()

For example:

moment(1451624400000).tz('America/New_York').isDST(); // false
moment(1467345600000).tz('America/New_York').isDST(); // true

Or, if your timestamps are already strings in terms of the local time, then use this syntax instead:

moment.tz('2016-01-01T00:00:00','America/New_York').isDST(); // false
moment.tz('2016-07-01T00:00:00','America/New_York').isDST(); // true
like image 69
Matt Johnson-Pint Avatar answered Sep 24 '22 20:09

Matt Johnson-Pint