0

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 ?

aliyasineser
  • 186
  • 3
  • 15
  • 0124 is a timezone? – Jaromanda X Jul 25 '17 at 06:07
  • 1
    Create date objects and then use `Date.getTime()` to get value in milliseconds and perform your arithmetic operations. You can also refer following link to check how to create date with timezone: https://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s – Rajesh Jul 25 '17 at 06:11
  • 2
    @JaromandaX +0124 is Warsaw Mean Time --> short interesting read: https://en.wikipedia.org/wiki/UTC%2B01:24 – Mark Jul 25 '17 at 06:17

2 Answers2

1

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)
Phil
  • 157,677
  • 23
  • 242
  • 245
0

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

Pommesloch
  • 492
  • 5
  • 17