How to find time difference between two timezones using moment js

I am trying to find the time difference between seconds between two time zones using pulses.

oldTZ = moment.tz("America/New_York").format('Z'); newTZ = moment.tz("Asia/Calcutta").format('Z');

This gives meaning -04:00

and +5:30

; the difference is 9.5 hours.

I would like to split them by a few seconds and subtract them msNewTZ-msOldTZ

to give me:34200

Now I cannot subtract these two because they are strings. As a result, I'm not sure how to handle this.

Thank.

+3


source to share


1 answer


I think I succeeded:

var newTZ= moment().tz("Asia/Calcutta");
var oldTZ = moment().tz("America/New_York");
difference = newTZ.subtract(oldTZ, 'seconds')
difference = difference.format("HH:mm");
sec= difference.split(":");
timeInSec = (sec[0] * 60 * 60) + (sec[1] * 60);
console.log(timeInSec);

      



You should use .format(...)

after the completion of time calculations. You cannot use diff () either, you must use subtract (). After the 4th line, it's just a matter of basic math.

-1


source







All Articles