JavaScript,时区和夏时制 [英] JavaScript, time zones, and daylight savings time

查看:79
本文介绍了JavaScript,时区和夏时制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎收看本周的《 又一个时区问题》

我已经做了很多有关SO的阅读,操纵moment.js和date.js来帮助我,并且自从我开始尝试解决此问题以来,通常就感到沮丧,所以如果有人可以帮助或指出我关于SO的重复问题,我只是没有能够找到它,真是太棒了。

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.

我有一个页面。该页面显示一系列时间,例如:7:28、7:38、7:48。我知道这些是否是AM / PM。这些时间始终是美国/纽约(夏时制更改时,它们不会更改,因为它们对应的事件总是在该时间发生,而不管夏令时如何)。我们称它们为时间表。我想强调下一个时间。

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.


  • 这对于居住在美国/纽约的人们来说是微不足道的。

  • 对于居住在美国/洛杉矶的人们来说,这并不可怕(假设我的逻辑原理可行)。

    • 我可以将美国/洛杉矶的计算机的当前时间转换为UTC,然后确定美国/洛杉矶当前是否正在遵守夏令时,并确定是否美国/纽约应该为-0400或-0500,将其应用于UTC,然后进行比较。这有点痛,因为您仍然始终使用美国/洛杉矶的Date进行操作,而实际上并没有更改Date对象的时区,但是我有可靠的方法可以回滚(或向前)UTC的小时数时间。

    但是,当我尝试确定如果正在从根本不遵守夏令时的地区的计算机上观察夏令时

    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据我所知,只会为当前时区创建Date对象,然后根据该Date对象进行DST的任何确定。

    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.

    我是否在乎?无论如何,时代主要只与居住在美国/纽约的人们有关。我只是试图构建一个从另一个时区查看时有意义的应用程序,例如当它在country_without_DST中是凌晨3点,在美国/纽约是2PM时,时间表强调了2:05 PM事情将要发生,不是3:05 AM。

    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.

    推荐答案

    所有与时间的比较都应使用 getTime()您的实例。这将返回自UTC时代以来的毫秒数。 DST无关紧要。您将 getTime()值发送到服务器。然后,您的客户端脚本会将这个值转换回JavaScript Date 对象,如下所示:

    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);
    

    然后在 mydate 上使用任何方法显示日期如何。这有意义吗?我看不到有什么问题。我很乐意清理所有内容。

    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.

    编辑:

    只是要百分百清楚...

    Just to be 100% clear...

    如果两个不同的客户需要在不同的时区相互展示自己的行为,建议您仅使用(new Date())。getTime()中的值,然后将其保存到服务器。然后,服务器将此值发送给每个相应的客户端。然后,客户端负责在其自己的适当语言环境中显示它。

    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.

    此外,如果您想要一个适合获取时区和偏移量的库,则 getTimezoneOffset()被认为是flakey,您应该查看该库: http://www.pageloom.com/automatic-timezone-detection-with-javascript

    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

    这篇关于JavaScript,时区和夏时制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆