Search code examples
javascriptnode.jstimezonemomentjsmoment-timezone

moment-timezone : Not getting desired time while converting from one timezone to other


I would like to convert "2020-09-07T00:52:26.000Z" to 07-09-2020 14:22:26, but I was not getting the desired time while conversion.

Conversion needed: Asia/Kolkata to Asia/Hong_Kong

I tried:

const moment = require('moment-timezone');
var momentmm = moment("2020-09-07T00:52:26.000Z").utc();
var updatetimezone = moment.tz(momentmm, 'Asia/Hong_kong').format();
console.log("1. After Convert timezone", updatetimezone)

Result: 1. After convert timezone 2020-09-07T08:52:26+08:00

var str = moment("2020-09-07T00:52:26.000Z").tz('Asia/Hong_kong').utc().format('DD-MM-YYYY HH:mm:ss');
console.log("2. After Convert timezone", str)

Result: 2. After convert timezone 07-09-2020 00:52:26

Desired Result:

07-09-2020 14:22:26


Solution

  • var on = new Date("2020-09-07T00:52:26.000Z");
    var m = moment(on)
    var str = moment(m).format('DD-MM-YYYY HH:mm:ss ZZ');
    console.log(str)
    console.log("Converted timezone", moment.tz(str, 'Asia/Hong_Kong').format('DD-MM-YYYY HH:mm:ss'))
    

    That will do it..