JavaScript, time zones, and daylight savings time

后端 未结 2 637
一个人的身影
一个人的身影 2020-12-19 09:10

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

2条回答
  •  醉酒成梦
    2020-12-19 09:57

    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

提交回复
热议问题