Home > Blockchain >  javascript timezone conversion using momentjs
javascript timezone conversion using momentjs

Time:01-20

I have a date and time in UTC and a timezone I want to convert this date and time to the given timezone equivalent e.g. if UTC date is 2022-01-10T00:00:00.000Z that means 10 Jan 00:00 am and when I convert it to NewYork timezone equivalent output should be given as 2022-01-09T19:00:00.000Z that is 9 Jan 07:00 pm or 19:00 because it has -05:00 hour from UTC. how I can achieve that can anyone help? timezone can be any timezone. Thanks for your help.

like 
let date= '2022-01-10T00:00:00.000Z';
let timezone = 'America/New_York';

CodePudding user response:

You can use moment-timezone like this:

const moment = require('moment-timezone')

const date = '2022-01-10T00:00:00.000Z';
const timezone = 'America/New_York';

const convertedDate = moment.utc(date)  // create Moment object from date in UTC
  .tz(timezone)                        // convert to provided time zone
  .format('YYYY-MM-DD hh:mm:ss A')    // display in format

console.log(convertedDate)
// 2022-01-09 07:00:00 PM
  •  Tags:  
  • Related