Angular Moment : Moment Timezone 没有 America/New_York 的数据 [英] Angular Moment : Moment Timezone has no data for America/New_York

查看:42
本文介绍了Angular Moment : Moment Timezone 没有 America/New_York 的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Moment Timezone 没有 America/New_York 的数据.请参阅 http://momentjs.com/timezone/docs/#/data-loading/.

但是 America/New_Yorkmoment 的已知时区,但它仍然要求我添加时区.

解决方案

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

  • 瞬间
  • 时刻时区
  • 时区数据用于时刻时区
  • 角力矩

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

  • moment-timezone.js 只是 脚本.它不包含时区数据.如果您使用此发行版,预计您将通过 moment.tz.addmoment.tz.load 获取您自己的时区数据,如所述在文档中.

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

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

每个都有缩小版本.

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

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

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

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

解决方案

You need to load all of the following:

  • moment
  • moment-timezone
  • time zone data for moment-timezone
  • angular-moment

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

  • moment-timezone.js is just the 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.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 truncated to just years 2012 through 2022. This is a much smaller data file, and is sufficient for the majority of browser-side applications.

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.

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.

这篇关于Angular Moment : Moment Timezone 没有 America/New_York 的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆