Search code examples
javascriptdatetimetimedatediff

how can I subtract time from time for time remaining?


Using JavaScript, How can I subtract from HH:MM:SS?

For example, I have 12:54:45 and I want to subtract 11:35:53 for remaining time in 4732 seconds


Solution

  • var first = (new Date('Jan 01, 2000 12:54:45')).getTime();
    var second = (new Date('Jan 01, 2000 11:35:53')).getTime(); 
    var differenceInSeconds = (second - first) / 1000;
    // 4732
    

    I chose 2000-01-01 as the date, as it does not matter as long it is the same. We only want to calculate the difference.