NodeJS

MomentJS Convert to desired timezone

clocks, time, idea-257911.jpg

Using Moment timezone library we can convert from UTC or any timezone to desired timezone. Moment timezone library supercedes the moment library where we can use the functions defined within the moment library

npm i moment-timezone

var moment = require('moment-timezone')

let originDate = moment(new Date()).utc().format("YYYY-MM-DD HH:mm:ss").toString()
let m = moment.utc(originDate);
m.tz('America/Chicago');
let convertedDate = m.format("YYYY-MM-DD HH:mm:ss");

We can also convert from a specific timezone instead of UTC

let fromTimezone = moment.tz("2023-10-08T10:00:00", "America/Los_Angeles")
let toTimezone = fromTimezone.tz("America/Los_Angeles").format("YYYY-MM-DD HH:mm:ss")

3 Comments on “MomentJS Convert to desired timezone

Leave a Reply to Ginette Berry Cancel reply

Your email address will not be published. Required fields are marked *