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
Angular Moment : Moment Timezone has no data for America/New_York
提问by Aman Gupta
Date received from server is in UTC
timezone 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_York
is a known timezone to moment
but 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.js
is 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 bymoment.tz.add
, ormoment.tz.load
, as described in the docs.moment-timezone-with-data.js
includes all known time zone data from the tz database, for the version mentioned on the web site.moment-timezone-with-data-2012-2022.js
includes 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_York
is a valid TZ database identifier, then you simply haven't loaded the data for it. You are probably using moment-timezone.js
without 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.js
or 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.js
或moment-timezone-with-data-2012-2022.js
,则不应使用moment-timezone.js
,因为该脚本已包含在内。