5

I'm trying to calculate the difference (in seconds) between two EPOCH timestamps using Javascript.

I have two timestamps:

START: 1565628094441
END: 1565714527812

Both timestamps were obtained using:

var time = new Date().getTime().toString();

I would like to determine the difference between both timestamps in seconds. I understand that Javascript returns epoch in milliseconds but I still haven't been able to find a way to convert them to seconds properly.

Here is what I've tried:

var differenceSec = (END- START)/1000;
// This gives us 86433.371 Seconds (~1440 minutes). 

Using Epoch Converter (https://www.epochconverter.com/#tools) I know that the true difference should be around 30 seconds. What am I doing wrong here?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Nothing was passed in when creating the dates... in order to get the current time: var time = new Date().getTime().toString(); – Nathaniel Deshpande Aug 12 '19 at 19:01
  • 1
    javascript is right, not some random website. i can tell by looking that those are more than 30 seconds apart. – dandavis Aug 12 '19 at 19:02
  • There are 86400 seconds in a day. So I think epoch is dropping the day difference and just telling you the 30 seconds part – ControlAltDel Aug 12 '19 at 19:02
  • there is a day difference, START is aug 12, and END is aug 13 – depperm Aug 12 '19 at 19:03
  • I've also done some testing using the toString method on the Date object. e.g. new Date(1565628094441).toString() and it matches the results of Epoch Converter. – Nathaniel Deshpande Aug 12 '19 at 19:05
  • 1
    https://stackoverflow.com/questions/13894632/get-time-difference-between-two-dates-in-seconds already answered – Dr G. Aug 12 '19 at 19:06
  • Possible duplicate of [Get time difference between two dates in seconds](https://stackoverflow.com/questions/13894632/get-time-difference-between-two-dates-in-seconds) – Don't Panic Aug 12 '19 at 19:11
  • how do you `know that the true difference should be around 30 seconds.`? – depperm Aug 12 '19 at 19:13
  • I've made an error - the two dates were generated a day apart... therefor the calculation is correct as pointed out by @depperm – Nathaniel Deshpande Aug 12 '19 at 19:17

1 Answers1

5

There are 24 * 60 * 60 = 86400 seconds in a day. Your figure of 86433.371 - 1 day = 33.371 seconds. So if Epoch Converter is not counting days, you have your answer

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I'm fairly certain the OP just missed the day, epoch converter is probably not doing anything wrong – depperm Aug 12 '19 at 19:08