admin管理员组

文章数量:1323225

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?

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?

Share Improve this question edited Feb 8, 2013 at 21:11 Matt Johnson-Pint 242k75 gold badges465 silver badges610 bronze badges asked Feb 8, 2013 at 19:54 user1756980user1756980 4,0074 gold badges18 silver badges13 bronze badges 11
  • is this a duplicate: stackoverflow./questions/4903722/… – kmote Commented Feb 8, 2013 at 20:06
  • this is actually quite easy, you just need to do a few date parisons, see my repo here for an extended example: github./jthoburn/Date.toString.js – runspired Commented Feb 8, 2013 at 20:16
  • specifically, look at the code for 'I' and modify it to use this method: stackoverflow./a/14636431/1883464 – runspired Commented Feb 8, 2013 at 20:21
  • 1 @runspired - Please see my ments on your library here. Thanks. – Matt Johnson-Pint Commented Feb 8, 2013 at 21:03
  • 1 @MattJohnson I have both, "America/New_York" and gmt -5, dst -4 – user1756980 Commented Feb 8, 2013 at 21:19
 |  Show 6 more ments

3 Answers 3

Reset to default 3

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 ments 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, remending 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 remend 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

Is there something I'm missing? Assuming I can determine whether DST is in effect, would this work where timezone_offset is the requested time zone's offset?

Try to always use the UTC methods to get attributes of dates. That way your (server's) local timezone will not affect any calculations:

var d = new Date(ts + timezone_offset);
d.getUTCMonth();
d.getUTCDate();

How might I determine whether DST is in effect?

That's plicated. There are/were many different algorithms to determine DST beginning and end, depending on the region - have a look at http://en.wikipedia/wiki/Daylight_saving_time_by_country for example.

I don't know whether there are any libraries that have already coded these, or maybe even contact the timezone database for correct information. The last resort would be to ask the user himself for timezone details.

moment.js (http://momentjs./) has a isDST function. You could use moment (lots simpler), or check how it is implemented.

moment(your_date).isDST();

本文标签: nodejsJavascript Datehow to know whether DST is in effect in a given timezoneStack Overflow