首页 > 解决方案 > Convert ISO/UTC date to local by displaying timezone javascript/momentjs

问题描述

I have a date in the format like 2019-05-18T19:30:00-0400 which I need to display as 05/18/2019 07:30 PM EST using momentjs or plain javascript. I tried several ways, for example, moment(new Date('2019-05-18T19:30:00-0400')).format('MM-DD-YYYY h:mm A zz')

标签: javascriptdatetimetimezonemomentjs

解决方案


仅从偏移量是不可能确定时区的。有许多偏移量属于多个时区。因此,呈现缩写是不可能的。

有关详细信息,请参阅 timezone 标记 wiki 中的Time Zone != Offset ,如果您需要示例,请参阅tz 数据库时区列表。

但是,从您的评论来看,您似乎还希望保持与给定相同的本地时间和偏移量。Moment 可以使用它的(名称不恰当的)parseZone函数来帮助解决这部分问题:

moment.parseZone("2019-05-18T19:30:00+0100").format("MM/DD/YYYY hh:mm A ZZ")
//=> "05/18/2019 07:30 PM +0100"

推荐阅读