【发布时间】:2015-09-09 04:13:09
【问题描述】:
我查看了这些帖子 - 以及阅读文档; Create a local date from a UTC date string in Moment.js 和 Changing the Utc Date to Local Date using moment.js
我仍然没有放弃得到我想要的东西。
数据库中的所有日期都以 UTC 格式存储,但我希望客户查看其本地时间的日期 - 但带有时区。
我可以获取 .local(),但这会减少区域信息,如果我指定时区,我可以获得我想要的结果,但我不会知道时区...不是那一件事时刻/时区应该给?我可以得到偏移量 - 但文档没有说明如何使用它。
示例(这就是我想要的):9 月 8 日 15 日,晚上 10:51 CDT
这是我尝试过的示例。
// I spoof a date as if it came from the db (for demo purposes)
var now = new Date().toUTCString();
// now outputs.. I am in CDT (-0500)
document.write( now );
// Wed, 09 Sep 2015 03:51:15 GMT
document.write( moment( now ).format('MMM Do YY, h:mm a z') );
// Sep 8th 15, 10:51 pm
document.write( moment( now ).local().format('MMM Do YY, h:mm a z') );
// Sep 8th 15, 10:51 pm (same as above, so I'm unclear what 'local()' gives me that I didn't already have?)
document.write( moment( now ).tz('America/Chicago').format('MMM Do YY, h:mm a z') );
// Sep 8th 15, 10:51:15 pm CDT
最后一个是我所追求的......但我不知道 tz,所以我什至不能将它设置为变量并将其传递,我如何获取时区作为字符串,而不是偏移量- 或者我怎样才能使用偏移量来做同样的事情? // CDT 15 年 9 月 8 日晚上 10:51
【问题讨论】:
标签: javascript momentjs