Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

timezone - JavaScript, time zones, and daylight savings time

Welcome to this week's episode of Yet Another Time Zone Question:

I've done a fair bit of reading on SO, tried to manipulate moment.js and date.js into helping me, and generally been plagued by a feeling of frustration since I started trying to solve this, so if someone could help or point me at the duplicate question on SO that I just haven't been able to find, that'd be awesome.


I have a page. This page displays a series of times, e.g.: 7:28, 7:38, 7:48. I know whether these are AM/PM. These times are always America/New York (they do not change when daylight savings time changes, as the event they correspond to always happens at that time regardless of DST). Let's call them a schedule. I want to highlight the time that is coming up next.

  • This is trivial for people living in America/New York.
  • This is not too terrible for people living in America/Los Angeles (assuming my logic works).
    • I can take the current time of the computer in America/Los Angeles, convert it to UTC, then determine if America/Los Angeles is currently observing DST or not and determine whether America/New York should be -0400 or -0500, apply that to the UTC, and make my comparison. This hurts a little because you're still always dealing with a Date based in America/Los Angeles and not actually changing the time zone of the Date object, but I have a reliable means of rolling back (or forward) the hours from the UTC time.

What happens, however, when I try to determine if daylight savings time is being observed from a computer in a region that does not observe daylight savings time at all?

JavaScript will only create Date objects for the current time zone, to my knowledge, and then doing any determination of DST is based on that Date object.

Should I just not care? The times are primarily relevant only to people living in America/New York anyway. I'm just trying to build an application that makes sense when viewed from another time zone such that when it's 3AM in country_without_DST and it's 2PM in America/New York, the 'schedule' highlights that the 2:05PM thing is about to happen and not the 3:05AM thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All comparisons with time should be done with getTime() of your instance. This returns the number of milliseconds since the UTC epoch. DST won't matter. You send the getTime() value to your server. Your clientside scripts will then convert this value back into a JavaScript Date object like so:

mydate = new Date(longmillisFromAnotherTZ);

Then use any method on mydate to display the date how you'd like. Does this make sense? I'm failing to see how there's an issue. I'd be happy to clear anything up though.

Edit:

Just to be 100% clear...

If two different clients need to display their actions to each other in different time zones, I'm suggesting that you use only the value from (new Date()).getTime() and then save that to the server. The server then sends this value to each respective client. The client is then responsible for displaying it in its own appropriate locale.

Also, if you want a library that is good for getting timezones and offsets, getTimezoneOffset() is known to be flakey, you should check out this library: http://www.pageloom.com/automatic-timezone-detection-with-javascript


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...