user3758858
user3758858

Reputation: 1

How to get the difference in miliseconds between two Timestamps which are of different timezone in Javascript

i wanted to get the difference of two timestamps of different timezone in javascript:

var firstTime ="2017-07-21 04:34:21"; 
var timeZone1:"+0100"; 
var secondTime ="2017-07-21 04:36:27";     
var timeZone2:"+0124";    

How we can get the difference in miliseconds ?

Upvotes: 0

Views: 60

Answers (2)

Phil
Phil

Reputation: 164795

Convert your strings into ISO 8601 formats. These generally work well in JavaScript Date constructors. Then simply subtract one from the other

var firstTime ="2017-07-21T04:34:21"; 
var timeZone1 = "+01:00"; 
var secondTime = "2017-07-21T04:36:27";     
var timeZone2 = "+01:24";  

const firstDate  = new Date(firstTime + timeZone1)
const secondDate = new Date(secondTime + timeZone2)
console.log('firstDate', firstDate)
console.log('secondDate', secondDate)
console.info('diff', firstDate - secondDate)

Upvotes: 1

Pommesloch
Pommesloch

Reputation: 502

you can use moment.js (https://momentjs.com/) for get the current timestamp from different time zone.

But don´t forget : "Time Zone != Offset" https://stackoverflow.com/tags/timezone/info

Another good example: Get timezone difference in hours with moment

Upvotes: 0

Related Questions