处理时区转换的JavaScript库 [英] JavaScript library that handles time zone conversion

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

问题描述

是否有可以处理时区转换的JavaScript库(并考虑DST规则和这样的内容)?我知道有类似的问题,但我看不到的都似乎有一个真正适合我的问题的答案。



我想在时区A中创建一个日期,并能够操作它(添加几天,几小时和类似的东西),然后将其转换为另一个时区B.必须有很多人需要这种功能,所以我猜应该有一些没有找到的库。

解决方案

我想你想要这样:



https ://github.com/mde/timezone-js



解析器确实需要一点调整才能在没有其他库的情况下运行放在一起(至少最后我使用它..五个月前?),但否则这似乎是一个坚实的图书馆。我已经使用它在两个非常时间敏感,高流量的商业网站上取得了巨大的成功。



有时候,jquery的日期选择器和momentjs(和其他库)可能不太好,所以准备调整这些库以使用timezoneJS的日期,或者具有转换功能,以便您可以将timezonejs日期转换为正常日期,反之亦然。输入/输出。



否则,作为例如,这将采用亚洲/新加坡时区的日期,并将其变为美国/ Los_Angeles日期,同时尊重该年度的夏令时。它使用Olson时区文件,它们是免费提供的,并将它们解析成实际上非常准确的东西。



希望这可以让您处于正确的路径。 >

- 编辑 -



忘了一个使用它的几个提示。如果您有一个有限的时区,我强烈建议您通过告诉解析器仅使用这些时间段,然后您不必加载所有这些文件。另外,由于setHours,setMinutes等不遵循setHours(0,0,0,0)(这是小时,分钟,秒,毫秒)的JSspec,我被踢了一次。这里有一个增强请求: https://github.com/mde/timezone-js/issues/48我不认为代码修改会很难支持,所以我希望我们会看到它比以后更早地合并,或者我可能会自己做;)


Is there a javascript library that can handle time zone conversions (and takes DST rules and such stuff into account)? I know there are similar questions, but none of the ones I have seen seem to have an answer that really fits my question.

I would like to create a date in time zone A and be able to manipulate it (add days, hours and stuff like that) and then convert it to another time zone B. There must be a lot of people that need this kind of functionality, so I guess there should be some library out there that I haven't found.

解决方案

I think you want this:

https://github.com/mde/timezone-js

The parser does need a bit of a tweak to get to run without the other library the person put together (at least last I used it.. five months ago?), but otherwise this seems to be a solid library. I've used it on two very timezone sensitive, high traffic, commercial sites with great success.

It doesn't play well with jquery's date picker and momentjs (and probably other libraries) sometimes though, so be prepared to either tweak those libraries to use timezoneJS' date, or have a to/from conversion function so that you can turn a timezonejs date into a 'normal' date and vice-versa for input/output.

Otherwise, as a sample case, this will take an Asia/Singapore timezone date and turn it into an America/Los_Angeles date while respecting the daylight savings time for that year. It uses Olson timezone files, which are freely available, and parses them out into something that is actually really accurate.

Hopefully this gets you on the right path.

--Edit--

Forgot - a couple tips on using it. If you've got a limited set of timezones, I'd strongly recommend plucking those out by telling the parser to only use those - then you don't have to load a big file of all of them. Also, I got kicked once because setHours, setMinutes, etc don't follow the JS "spec" of setHours(0, 0, 0, 0) (which is hours, minutes, seconds, millis). There is an enhancement request here: https://github.com/mde/timezone-js/issues/48 I don't think that it will be at all difficult for the code modification to support that, so I hope we'll see it merged in sooner than later.. or I may just do it myself ;)

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

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