Calculate the difference in seconds between two epoch timestamps in Javascript

11,440

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

Share:
11,440
Nathaniel Deshpande
Author by

Nathaniel Deshpande

Updated on June 22, 2022

Comments

  • Nathaniel Deshpande
    Nathaniel Deshpande about 2 years

    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?