简体   繁体   中英

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.

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.

 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. (Some of the month names cannot be parsed with "en" locale).

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.

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)

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.

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".

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.

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. It has been mis-applied to define the format of dates and in moment.js documentation to mean language (sometimes).

Anyway, if you want to parse strings with different languages, use moment.js with locales (languages).

eg with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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