简体   繁体   English

moment.js解析语言和时区中的日期

[英]moment.js parse date in language and timezone

I want to convert date in different timezone that I have as string to a ISO date. 我想将字符串形式的其他时区中的日期转换为ISO日期。

What works: parsing a non-localized date in moment with a timezone: 什么有效:解析具有时区的瞬间的非本地化日期:

 var sdate = "2016-12-13 09:45:46" var m = moment.tz(sdate, "Europe/Amsterdam"); var iso = m.toISOString(); console.log(iso); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js"></script> 

But when I have a date that in german ("de" locale), and that has timezone, then I cannot do anything. 但是,当我有一个以德语(“ de”语言环境)命名的日期并且有时区时,那么我什么也做不了。

 var sDateRaw = "16. Aug. 2017 12:21" var m = moment.utc(sDateRaw, "DD. MMM. YYYY hh:mm", "de"); var iso = m.toISOString(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js"></script> 

In the second case, I need to supply "de" locale, because the Month strings require the locale. 在第二种情况下,我需要提供“ de”语言环境,因为Month字符串需要该语言环境。 (Some of the month names cannot be parsed with "en" locale). (某些月份的名称无法使用“ en”语言环境进行解析)。

The string date is in timezone "Europe/Amsterdam". 字符串日期在时区“欧洲/阿姆斯特丹”中。 However in the parsing routine, I nowhere can set the timezone that the string date is in. 但是,在解析例程中,我无处可设置字符串日期所在的时区。

If I want the same result, independent of where the machine is located in; 如果我希望得到相同的结果,则与机器所在的位置无关; I have to use utc to parse it. 我必须使用utc进行解析。

It seems that moment is missing a option to parse dates with locale and timezone at the same time. 似乎此刻缺少同时解析带有区域设置和时区的日期的选项。

Side note: Since moment is a singleton; 旁注:由于力矩是单例; ( all require("moment") do get back the same object), I do not want to set locale or timezone in moment, because this then can impact other parts of the application over which I do not have any control over (many libraries use moment) (所有require(“ moment”)都返回相同的对象),我不想立即设置区域设置或时区,因为这可能会影响我无法控制的应用程序的其他部分(许多库使用时刻)

So to solve my problem, I can only see the solution, to now print the date as a iso string, and then parse the iso string with supplying the timezone. 因此,要解决我的问题,我只能看到解决方案,现在将日期打印为iso字符串,然后通过提供时区来解析iso字符串。

But doing date parsing two times, this does not really make sense to me. 但是对日期进行两次解析,这对我来说真的没有意义。

"Locales" only work if you use moment with locales. “语言环境”仅在您将提示与语言环境一起使用时才有效。 The moment.js documentation is poor in that it is very brief and confuses the term "locale" with "language". moment.js文档很糟糕,因为它非常简短,并且将“语言环境”与“语言”混淆了。

When parsing a string, you should pass: 解析字符串时,应传递:

  1. the string to parse 要解析的字符串
  2. the format, and optionally 格式,以及可选的
  3. the language of the string using an ISO 639 language code. 使用ISO 639语言代码的字符串语言。

This third parameter is called "locale" in the documentation, but it is actually a language and should not be confused with the other uses (often inappropriate) of "locale" in regard to dates, times, timezones and formats. 第三个参数在文档中称为“语言环境”,但实际上是一种语言,不应与“语言环境”在日期,时间,时区和格式方面的其他用法(通常不适当)相混淆。

The term "locale" means a small geographic region, like a suburb or village. 术语“语言环境”是指较小的地理区域,例如郊区或村庄。 It is more usually associated with tz timezone designators like Africa/Cairo, which really is a locality or locale. 它通常与诸如非洲/开罗之类的tz时区指示符相关联,这实际上是一个地区或地区。 It has been mis-applied to define the format of dates and in moment.js documentation to mean language (sometimes). 定义日期的格式以及在moment.js文档中定义语言(有时)是错误的。

Anyway, if you want to parse strings with different languages, use moment.js with locales (languages). 无论如何,如果要解析具有不同语言的字符串,请使用带有语言环境(语言)的moment.js。

eg with moment-with-locales.min.js : 例如,带有moment-with-locales.min.js

 var sDateRaw = "16. Aug. 2017 12:21" var m = moment.utc(sDateRaw, "DD. MMM. YYYY hh:mm", "de"); var iso = m.toISOString(); console.log(iso); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js"></script> 

To use times across multiple locations, keep everything in UTC and only use local times for display. 要在多个位置使用时间,请将所有内容保留在UTC中,并且仅使用本地时间进行显示。

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

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