Convert a specific date and time between locations

2019-07-27 08:44发布

问题:

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')