简体   繁体   English

通过显示时区javascript / momentjs将ISO / UTC日期转换为本地

[英]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. 我的日期格式为2019-05-18T19:30:00-0400,我需要使用momentjs或普通javascript在美国东部时间 05/18/2019 07:30 PM显示。 I tried several ways, for example, moment(new Date('2019-05-18T19:30:00-0400')).format('MM-DD-YYYY h:mm A zz') 我尝试了几种方法,例如,时刻(新日期('2019-05-18T19:30:00-0400'))。格式('MM-DD-YYYY h:mm A zz')

It is impossible to determine a time zone from an offset alone. 不可能仅从偏移确定时区。 There are many offsets that belong to more than one time zone. 有许多偏移属于多个时区。 Thus rendering an abbreviation is impossible. 因此,缩写是不可能的。

See Time Zone != Offset in the timezone tag wiki for further details, and refer to the list of tz database time zones if you would like examples. 有关详细信息,请参阅timezone != Offset in timezone标记wiki,如果您需要示例,请参阅tz数据库时区列表

However, from your comments it appears you that you would also like to keep the same local time and offset as you are given. 但是,根据您的评论,您似乎还希望保留与给定时相同的本地时间和偏移量。 Moment can help with that part, using its (inappropriately named) parseZone function: 使用其(不恰当命名的) parseZone函数,Moment可以帮助完成该部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM