简体   繁体   English

为什么javascript几乎不支持时区?

[英]Why does javascript has a little or no support for timezones?

I was looking for something in javascript which could support time zones. 我一直在寻找可以支持时区的JavaScript语言。 My motive was: 我的动机是:

  1. Seamless conversions between two time zones. 两个时区之间的无缝转换。
  2. Construct Date objects with distinct time zones from milliseconds. 构造具有不同时区(以毫秒为单位)的Date对象。
  3. Formatting Dates according to different zones and locales. 根据不同的区域和区域设置日期格式。

Currently I am achieving #1 by constructing back a new Date object from locale String as: 目前,我通过从语言环境String重新构造一个新的Date对象来实现#1:

new Date(dateInSomeTimeZone.toLocaleString('en-US', {timeZone: 'Asia/Calcutta'})

This again is bad because the millis representation of this Date is different from that of the original. 这再次很不好,因为此Date的毫秒表示不同于原始日期。 Also there are ways for achieving #3, but #2 appears to be a 'Not achievable'. 还有一些方法可以实现#3,但是#2似乎是“无法实现的”。

Is there any reason why a mature language like JS doesn't provide this feature which is supported by most of the frameworks? 是否有任何原因导致像JS这样的成熟语言不提供大多数框架支持的此功能? I know there are libraries like Moment.js doing these things, but integrating a new library is not always possible in already running applications. 我知道有像Moment.js这样的库在做这些事情,但是在已经运行的应用程序中并不总是可以集成一个新库。

If there are solutions to the points mentioned above? 是否有解决上述问题的方法?

The main problem you're running into is that JavaScript is a client-side scripting language. 您遇到的主要问题是JavaScript是一种客户端脚本语言。 That means that JavaScript only uses the time the computer thinks is correct. 这意味着JavaScript仅使用计算机认为正确的时间。 Slight differences to these time stamps occur because the computer isn't always synced perfectly with the real time. 这些时间戳略有差异是因为计算机并非总是与实时完美同步。

Support for time zones is very minimal because you can not always trust the settings for time zones on the different computers. 对时区的支持非常少,因为您不能始终信任其他计算机上的时区设置。

Besides from the reasons why, the solution you already have at the moment is the one I would suggest to use. 除了原因之外,您目前已经拥有的解决方案是我建议使用的解决方案。 Execution time of that code might be a problem but I think that adding a library for this would take even longer. 该代码的执行时间可能是个问题,但我认为为此添加库将花费更长的时间。

I agree that JavaScript's native date and time APIs are far from ideal, and moment.js is probably the best answer right now for most folks. 我同意JavaScript的本机日期和时间API远非理想,而且现在对大多数人来说, moment.js可能是最佳答案。

However, there are some things that are improving, as part of the ECMAScript Internationalization API (ECMA-402). 但是,作为ECMAScript国际化API (ECMA-402)的一部分, 有些事情正在改进。 Parts of the 1st edition are already implemented in some browsers, and the current 2nd edition will hopefully be rolled out over time. 第一版的某些部分已经在某些浏览器中实现,并且当前的第二版有望随着时间的推移而推出。

The example you gave in the question, using toLocaleString is covered by this API. 您在问题中给出的使用toLocaleString的示例已包含在此API中。 However, time zone support is lacking in many implementations. 但是,在许多实现中缺少时区支持。 For example you showed using a time zone ID, which will work on Chrome but not on Internet Explorer. 例如,您使用时区ID进行显示,该ID在Chrome上有效,但在Internet Explorer上无效。

Additionally, ECMA-402 is focused on your third scenario. 此外,ECMA-402专注于您的第三种情况。 The other two would require much more changes than are currently proposed. 其他两个将需要比当前建议的更改更多的更改。

As to why , consider that much of the JavaScript Date object is similar in design to the original java.util.Date from Java's JDK 1.0. 至于为什么 ,请考虑许多JavaScript Date对象在设计上与Java JDK 1.0中的原始java.util.Date类似。 When JDK 1.1 came around, much of that API was deprecated and moved to java.util.Calendar . 当JDK 1.1出现时,该API的大部分已被弃用并移至java.util.Calendar When you compare the two Java APIs to the JavaScript Date object with ECMA-402 applied, you'll see they are very similar. 当您将两个Java API与应用了ECMA-402的JavaScript Date对象进行比较时,您会发现它们非常相似。

Now consider that Java developers still had lots of problems with both Date and Calendar objects, and from that frustration grew the Joda-Time library. 现在考虑Java开发人员在DateCalendar对象方面仍然存在很多问题,并且从这种挫败感中发展出了Joda-Time库。 Then eventually, those concepts were adopted and improved on in Java 8's java.time package . 最终,这些概念在Java 8的java.time包中被采用和改进。 That is a very mature and well-thought-out API, and probably they type of thing you were looking for in JavaScript. 那是一个非常成熟且经过深思熟虑的API,可能它们是您在JavaScript中寻找的东西。 One can only hope that it won't take the 17 years it took Java to evolve. 只能希望Java不需要花17年的时间就能发展起来。

AFAIK, there are no drastic date/time improvements of this sort in ES6/2015, or planned for ES7/2016. AFAIK,在ES6 / 2015中或计划在ES7 / 2016中没有大幅度的日期/时间改进。 So in the meantime, it will come down to libraries. 因此,与此同时,它将归结为库。 moment is a good one. 片刻是个好时机。 There are other good ones, and plenty of bad, and I'm sure there will be more of both in the future. 还有其他好事,也有很多坏事,而且我敢肯定,将来两者都会更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM