How to convert from utc to another timezone using moment?

Member

by adan , in category: Javascript , 15 days ago

How to convert from utc to another timezone using moment?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 14 days 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.