Convert a specific date and time between locations using Moment.js version 2.0.0+

爷,独闯天下 提交于 2019-12-24 07:01:18

问题


How do I change a date time string that represents a New York, New York date and time to Melbourne Australia using Moment.js 2.0.0+ ?


Other answers I found on StackOverflow were outdated or slightly different than my use case.

convert this string '2016-04-28 09:30:00' to its Melbourne Australia equivalent 

回答1:


Simply:

var s = moment.tz('2016-04-28 09:30:00',      // input date time string
                  'YYYY-MM-DD HH:mm:ss',      // format of input
                  'America/New_York')         // time zone of input

              .tz('Australia/Melbourne')      // convert to another time zone

              .format('YYYY-MM-DD HH:mm:ss'); // format output string



回答2:


The solution I found is posted below

set the time zone to convert from

moment.tz.setDefault('America/New_York');

create the moment object with date and time as well as date format and set the timezone of the date

var ny = moment('2016-04-28 09:30:00','YYYY-MM-DD HH:mm:ss').tz('America/New_York');

clone the original time and date then change the time zone

var mel = ny.clone().tz('Australia/Melbourne');

use the .format() function to see the date in the console

mel.format('YYYY-MM-DD HH:mm:ss')


来源:https://stackoverflow.com/questions/44043069/convert-a-specific-date-and-time-between-locations-using-moment-js-version-2-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!