0

In JavaScript, how can I calculate the difference between two times that are in 24 hour format which are having different dates?

Example: Date1 is 2019/12/31 11:00:06 AM Date2 is 2020/01/01 01:10:07 PM. Time difference should be 02:10:13 in hh:MM:ss format ..how can get like this when date changes in appscript

TheMaster
  • 45,448
  • 6
  • 62
  • 85
VincentB
  • 13
  • 6
  • 2
    You use a datetime library rather than trying to figure things out yourself. Because the difference you gave is entirely incorrect, given that you forgot to indicate the timezones involved. – Mike 'Pomax' Kamermans Jan 06 '20 at 16:40
  • Does this answer your question? [get the time difference between two times in format hh:mm:ss](https://stackoverflow.com/questions/47173462/get-the-time-difference-between-two-times-in-format-hhmmss) – Heretic Monkey Jan 06 '20 at 16:42
  • @Heretic Monkey.... I want get the time difference between two times in two different dates...your solution getting two different times on same day – VincentB Jan 06 '20 at 16:49
  • count the difference between two dates in miliseconds and count amount of years months days hours minutes and seconds from it – Xesenix Jan 06 '20 at 16:51
  • @VincentB There are thousands of questions on Stack Overflow dealing with dates. Pick one. https://stackoverflow.com/questions/7709803/javascript-get-minutes-between-two-dates perhaps. – Heretic Monkey Jan 06 '20 at 16:53
  • Adding to Mike's comment. I suggest using tried and tested libraries like moment.js. Heretic monkey's link has a moment.js answer. Exceptionally neat – SamwellTarly Jan 06 '20 at 16:56
  • Isn't 1pm ~ 13 in hours when in 24hr format? xD – Jason Is My Name Jan 07 '20 at 14:47

2 Answers2

1

Just use the Date

const dateDiffMs = (date1,date2 ) => {
    const d1 = new Date(date1);
    const d2 = new Date(date2);
    return d1.getTime() - d2.getTime()
}

const ms2hms = (ms) => {
    const sec = Math.floor(ms / 1000)
    const min = Math.floor(sec / 60)
    const h = Math.floor(min / 60)

    return [
        h,
        min % 60,
        sec % 60,
    ];
};

const format = (n) => n < 10 ? '0' + n : n;

const hms2str = ([h, min, sec]) => {
    return `${h}:${format(min)}:${format(sec)}`
}

alert(hms2str(ms2hms(dateDiffMs('2020/01/01 01:10:07 PM', '2019/12/31 11:00:06 AM')))); // 26:10:01

This code works correctly if both date1 and date2 are in the same timezone. But i would recommend you to use moment.js or some other library

dm.shpak
  • 335
  • 1
  • 7
  • The OP wants the difference in HH:MM:SS format. Your solution will only provide milliseconds between the times. I suggest to kindly enhance your answer – SamwellTarly Jan 06 '20 at 16:54
0

I would do this by gathering the date in second since whenever computers decided to keep track of time for us sometime in the 70's (epoch). Then pass it the second value and subtract, leaving the difference.

You would then need to convert it back to a date format I presume:

(function(){

 var dateOneSeconds = new Date().getTime() / 1000;

 setTimeout(function(){

  var dateTwoSeconds = new Date().getTime() / 1000;

  var seconds = dateTwoSeconds - dateOneSeconds;

  console.log(seconds);

  var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8);

  console.log(timeDifferenceInDate);

 }, 3000);

})();

NOTE: I have used a timeout function - you will already have two dates that do not match to pop in.

EDIT: having been notified the days will not be calculated, you could maybe use date to calculate your time in seconds then use Math to create your display:

(function(){

 var dateOneSeconds = new Date().getTime() / 1000;

 setTimeout(function(){

  var dateTwoSeconds = new Date().getTime() / 1000;

  var seconds = dateTwoSeconds - dateOneSeconds;

  console.log(seconds);

  /* var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8); */

  seconds = Number(seconds);
  var d = Math.floor(seconds / (3600*24));
  var h = Math.floor(seconds % (3600*24) / 3600);
  var m = Math.floor(seconds % 3600 / 60);
  var s = Math.floor(seconds % 60);

  timeDifferenceInDate = d + ':' + h + ':' + m + ':' + s;

  console.log(timeDifferenceInDate);

 }, 3000);

})();
halfer
  • 19,824
  • 17
  • 99
  • 186
Jason Is My Name
  • 929
  • 2
  • 14
  • 38