在angularjs中使用时区格式化日期 [英] Format date with timezone in angularjs
问题描述
使用angularjs 1.2.26我无法将日期输入格式设置为所需的时区,例如:http://plnkr.co/edit/CxCqoR3Awcl1NFrCZYjx?p=preview
Using angularjs 1.2.26 i cant format my date input to the desired timezone, here's an example: http://plnkr.co/edit/CxCqoR3Awcl1NFrCZYjx?p=preview
{{'2015-07-10T12:37:08Z' | date : "MMMM d, y 'at' h:mma Z" : 'America/Bahia'}}
和
{{'2015-07-10T12:37:08Z' | date : "MMMM d, y 'at' h:mma Z" : 'Europe/Paris'}}
根据我的浏览器的本地时区并忽略我设置的时区,给出相同的输出.
gave the same output respecting my browser's local timezone and ignoring the timezone i've set.
推荐答案
有两个问题:
- 时区支持看起来像是在1.2.26之后引入的
-
时区支持不会采用任意的TZDB ID.来自 v1.4.1
用于格式化的时区.它了解UTC/GMT和美国大陆时区的缩写,但对于一般用途,请使用时区偏移量,例如'+0430'(格林威治子午线以东4小时30分钟).如果未指定,则为浏览器将被使用.
Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used.
v1.3.16 中的限制更为严格:
用于格式化的时区.目前,仅支持"UTC".如果未指定,将使用浏览器的时区.
Timezone to be used for formatting. Right now, only 'UTC' is supported. If not specified, the timezone of the browser will be used.
因此,如果您将示例更改为使用:
So if you change your example to use:
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.2/angular.js" data-semver="1.4.2"></script>
然后:
<span>{{'2015-07-10T12:37:08' | date : "MMMM d, y 'at' h:mma Z" : 'PST'}}</span>
<br/>
<span>{{'2015-07-10T12:37:08' | date : "MMMM d, y 'at' h:mma Z" : 'EDT'}}</span>
(例如),您将获得以下输出:
(for example) then you'll get output of:
July 10, 2015 at 3:37AM -0800
July 10, 2015 at 7:37AM -0400
这篇关于在angularjs中使用时区格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!