How to convert back to another timezone in moment.js?

Member

by gilbert , in category: Javascript , a year ago

How to convert back to another timezone in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by lindsey.homenick , a year ago

@gilbert 

To convert a date and time back to another timezone in Moment.js, you can use the moment.tz() method and specify the target timezone as the argument. Here is an example:

1
2
3
4
5
6
7
// Convert current date and time to 'America/New_York' timezone
let dateTime = moment.tz('America/New_York');

// Convert 'America/New_York' timezone back to 'Asia/Tokyo' timezone
let dateTimeTokyo = dateTime.clone().tz('Asia/Tokyo');

console.log(dateTimeTokyo.format('YYYY-MM-DD HH:mm:ss')); // Output: Converted date and time in 'Asia/Tokyo' timezone


In this example, we first create a Moment.js object with the current date and time in the 'America/New_York' timezone. Then, we use the clone() method to create a copy of the Moment.js object and use the tz() method to convert it back to the 'Asia/Tokyo' timezone. Finally, we use the format() method to display the converted date and time in the desired format.

Related Threads:

How to convert from utc to another timezone using moment?
How to change moment.js timezone defaults?
How to remove 00 in timezone moment.js?
How to shorten the timezone name list from moment.js?
How to ignore timezone when parsing using moment.js?
How to convert minutes to hh:mm with moment.js?