How to convert from utc to another timezone using moment?

Member

by adan , in category: Javascript , a year ago

How to convert from utc to another timezone using moment?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , a year ago

@adan 

To convert a UTC date/time to another timezone using Moment.js, you can use the moment.utc() method to create a moment object from the UTC datetime and then use the tz() method to specify the desired timezone to convert to. Here's an example:

1
2
3
4
5
6
7
8
9
const moment = require('moment-timezone');

// UTC date/time
const utcDateTime = moment.utc('2022-06-15 12:00:00');

// Convert to another timezone (e.g. New York)
const newYorkDateTime = utcDateTime.tz('America/New_York');

console.log(newYorkDateTime.format()); // Output: 2022-06-15T08:00:00-04:00


In this example, the UTC datetime '2022-06-15 12:00:00' is converted to the New York timezone using the tz() method with the timezone identifier 'America/New_York'. The converted datetime is then printed in the console in the format specified by the format() method.

Related Threads:

How to convert back to another timezone in moment.js?
How to convert new date() to utc date using moment.js?
How to ignore timezone when parsing using moment.js?
How to convert sysdate to utc time in oracle?
How to convert mm:ss to seconds using moment.js?
How to convert unix timestamp to utc 0 format in mysql?