-1

I want to calculate time difference between two time zones using javascript.

var startTime = doc.data().startTime;
output: Fri Dec 06 2019 19:00:00 GMT+0530 (India Standard Time)

var currentTime = new Date(Date.now()).toString();
output: Fri Dec 06 2019 13:15:30 GMT+0530 (India Standard Time)

Now i want to calculate the difference between these two timezones.

Aman Singh
  • 195
  • 1
  • 3
  • 11
  • Does this answer your question? [Get the time difference between two datetimes](https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes) – stud3nt Dec 06 '19 at 08:00
  • 2
    Does this answer your question? [How Do I calculate the difference of 2 time zones in JavaScript?](https://stackoverflow.com/questions/29265389/how-do-i-calculate-the-difference-of-2-time-zones-in-javascript) – Hitesh Tripathi Dec 06 '19 at 08:03
  • whatever you shared there the format of date and time is different then what i have shared over here in my question. Still not able to figure it out – Aman Singh Dec 06 '19 at 09:17

2 Answers2

1

You can do something like the below code.

var date1 = new Date("2019-12-6 12:15:15");
    var date2 = new Date("2019-12-6 12:25:15");


    var diff =(date2.getTime() - date1.getTime()) ;
    var hours = Math.floor(diff / (1000 * 60 * 60));
    diff -= hours * (1000 * 60 * 60);
    var mins = Math.floor(diff / (1000 * 60));
    diff -= mins * (1000 * 60);
    
    console.log("Time:",hours +":"+mins)
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
  • i cant pass the date and time as harcoded what you have done. I have to pass it as a variable. Beacause in my database there are many timezones in this format and i have to calculate the time difference with the current time. – Aman Singh Dec 06 '19 at 09:19
0

Here is one way:

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffDays);
Muhammad Zeeshan
  • 4,608
  • 4
  • 15
  • 41