使用moment.js格式化日期时间以显示时区 [英] Format datetime with moment.js to show timezone

查看:1591
本文介绍了使用moment.js格式化日期时间以显示时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题用moment.js显示时区。

I have problem showing timezone with moment.js.

我尝试使用此代码:

var result = moment(someDate).format("MM/DD/YYYY HH:mm A Z");

我得到了回报,例如: 08/05/2015 06: 18 PM +02:00 ,这很好,但我希望我的输出像 08/05/2015 06:18 PM WEDT 或类似的东西,带有时区缩写。

and I get return, for example: 08/05/2015 06:18 PM +02:00, which is fine, but I want that my output be like 08/05/2015 06:18 PM WEDT or something like that, with abbreviations of timezones.

尝试使用此代码,但最后我得到空时区:

Tried using this code, but I get empty timezone on the end:

var result = moment(someDate).format("MM/DD/YYYY HH:mm A z"); 

var result = moment(someDate).format("MM/DD/YYYY HH:mm A zz");

UPDATE

所以@Matt Johnson建议,我使用这种方法使用 moment-timezone-with-data显示时区。 js tzdetect.js

So as @Matt Johnson suggested, I used this approach to show time zone using moment-timezone-with-data.js and tzdetect.js:

var tzName = tzdetect.matches()[0];
var result = moment.tz(myDate, tzName).format("MM/DD/YYYY h:mm A zz");


推荐答案

如上所述

As described in the documentation:


注意:从1.6.0开始,z / zz格式标记已被弃用。 在此处详细了解

一般问题是浏览器无法通过一致的API提供时区缩写。为了提供它们,必须有一个外部数据源。

The general problem is that time zone abbreviations are not available from the browser through a consistent API. In order to provide them, one has to have an external source of data.

您可能希望使用 moment-timezone addon。它提供时区信息,包括缩写。您必须知道您正在使用的特定时区。例如:

You may want to look into using the moment-timezone addon. It provides time zone information, including abbreviations. You would have to know the specific time zone you are working with. For example:

moment.tz("2015-08-05T00:00:00+01:00", "Europe/London").format("MM/DD/YYYY hh:mm A z");
// "08/05/2015 12:00 AM BST"

此外,你不应该与 A 混合 HH (24小时制的小时)(12小时上午/下午)标志)。使用 hh A ,或使用 HH 不用 A

Also, you shouldn't mix HH (hours of the 24-hour clock) with A (the 12-hour am/pm designator). Either use hh with A, or use HH without A.

这篇关于使用moment.js格式化日期时间以显示时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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