JavaScript, time zones and daylight saving time

Welcome to this episode. Another time zone question :

I did a small part of reading on SO, tried to manipulate moment.js and date.js to help me, and overall I was tormented by the feeling of frustration since I started trying to solve this problem, so if someone could help or point to me on a duplicate question about SO that I just could not find, that would be awesome.


I have a page. This page is displayed several times, for example: 7:28, 7:38, 7:48. I know this is AM / PM. These times are always America / New York (they do not change when daylight saving time changes, since the event that they correspond always occurs at this time, regardless of DST). Let me call them a schedule. I want to highlight the time that will come next.

  • This is trivial for people living in America / New York.
  • It's not too scary for people living in America / Los Angeles (assuming my logic works).
    • I can take the current computer time in America / Los Angeles, convert it to UTC, and then determine whether America / Los Angeles is currently watching DST or not, and determine whether America / New York should be -0400 or -0500, apply this to UTC and do my comparison. This is a little painful, because you still always have a date based in America / Los Angeles, and you don’t actually change the time zone of the Date object, but I do have a reliable way to tilt (or forward) the clock from UTC time.

What happens, however, when I try to determine if daylight saving time is respected from a computer in a region where daylight saving is not observed at all?

JavaScript will only create Date objects for the current time zone, as far as I know, and then any DST definition is based on that Date object.

I just don't care? Times are primarily relevant only to people living in America / New York. I'm just trying to create an application that makes sense when viewed from a different time zone, so when it is 3AM in country_without_DST and its 2PM in America / New York, the β€œgraph” emphasizes that the 2:05 PM thing should happen and not 3 : 05 a.m.

+6
source share
2 answers

All comparisons over time should be done using getTime() your instance. This returns the number of milliseconds since the UTC era. DST does not matter. You send the getTime() value to your server. Your client scripts then convert that value back to a JavaScript Date object, for example:

 mydate = new Date(longmillisFromAnotherTZ); 

Then use any method on mydate to display the date as you would like. Does this make sense? I do not see how the problem is there. I would be happy to clarify something.

Edit:

Just to be 100% clear ...

If two different clients should show their actions to each other in different time zones, I suggest you use only the value from (new Date()).getTime() , and then save it on the server. The server then sends this value to each corresponding client. The client is then responsible for displaying it in its respective locale.

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

+5
source

And, I think, I finally understood what you are saying. You want to say something like: "The current time in New York is _ " depending on the time on the user's computer, and despite the support (at least in the documents) for setTimezone("EDT") , not there seems to be support for setTimezone("America/New York") . You will have to either hardcode the transition dates between EDT and EST (based on the current GMT time that you can get from the user computer), or use a third-party API (or do it on the server side).

+4
source

Source: https://habr.com/ru/post/909869/


All Articles