Javascript Angular Moment : Moment Timezone 没有 America/New_York 的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33258908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:51:49  来源:igfitidea点击:

Angular Moment : Moment Timezone has no data for America/New_York

javascriptangularjstimezonemomentjsangular-moment

提问by Aman Gupta

Date received from server is in UTCtimezone and I need to convert it to a particular timezone, for example : America/New_York.Following is the code for same

从服务器收到的日期在UTC时区中,我需要将其转换为特定时区,例如:.America/New_York以下是相同的代码

  <span class="bold" ng-bind="sess.date_time | amTimezone:'America/New_York' | amDateFormat:'h:mm a'"></span>

But on doing so I get the following error:

但是这样做时,我收到以下错误:

Moment Timezone has no data for America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.

But America/New_Yorkis a known timezone to momentbut still it is asking me to add the Timezone.

但是America/New_York是一个已知的时区,moment但它仍然要求我添加时区。

回答by Matt Johnson-Pint

You need to load all of the following:

您需要加载以下所有内容:

  • moment
  • moment-timezone
  • time zone datafor moment-timezone
  • angular-moment
  • 片刻
  • 时区
  • 时区数据的时刻,时区
  • 角力矩

On the moment-timezonehome page, there are available three different distributions of moment-timezone.

moment-timezone主页上,提供了三种不同的 moment-timezone 分布。

  • moment-timezone.jsis justthe script. It contains no time zone data. If you use this distribution, it's expected that you will pull in your own time zone data, either by moment.tz.add, or moment.tz.load, as described in the docs.

  • moment-timezone-with-data.jsincludes all known time zone data from the tz database, for the version mentioned on the web site.

  • moment-timezone-with-data-2012-2022.jsincludes the same tz data, but is truncatedto just years 2012 through 2022. This is a much smaller data file, and is sufficient for the majority of browser-side applications.

  • moment-timezone.js只是脚本。它不包含时区数据。如果您使用此发行版,预计您将按照文档中的描述通过moment.tz.add、 或提取您自己的时区数据moment.tz.load

  • moment-timezone-with-data.js包括来自tz 数据库的所有已知时区数据,适用于网站上提到的版本。

  • moment-timezone-with-data-2012-2022.js包含相同的 tz 数据,但被截断到 2012 年到 2022 年。这是一个小得多的数据文件,对于大多数浏览器端应用程序来说已经足够了。

There are minified versions of each as well.

每个都有缩小版本。

So if you are getting "Moment Timezone has no data for America/New_York", since America/New_Yorkis a valid TZ database identifier, then you simply haven't loaded the data for it. You are probably using moment-timezone.jswithout adding data to it. Either include time zone data with moment.tz.add, or (more appropriately) switch to one of the files that already includes all time zone data.

所以如果你得到"Moment Timezone has no data for America/New_York", 因为America/New_York是一个有效的 TZ 数据库标识符,那么你只是没有为它加载数据。您可能正在使用moment-timezone.js而不向其添加数据。使用 包含时区数据moment.tz.add,或者(更恰当地)切换到已包含所有时区数据的文件之一。

However, do not do both. Time zone data should only be loaded once, and the moment-timezone scripts should only be loaded once. If you use either moment-timezone-with-data.jsor moment-timezone-with-data-2012-2022.js, you should not be using moment-timezone.js, as that script is already included.

但是,不要两者都做。时区数据应该只加载一次,而 moment-timezone 脚本应该只加载一次。如果您使用moment-timezone-with-data.jsmoment-timezone-with-data-2012-2022.js,则不应使用moment-timezone.js,因为该脚本已包含在内。