Copy link to clipboard
Copied
I would like to calculate the difference between 2 timestamps. The timestamp is generated from the new Date() method and is formatted as hh:mm:ss in military time.
What needs to take place to calculate the difference between 2 timestamps?
Does the time stamp need to be converted to seconds to calculate the difference and then reconvert to the hh:mm:ss time format?
function militaryTime() {
// Get the time and format it
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
//military format
if (hours <= 9) hours = "0" + hours;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
var time = hours + ":" + minutes + ":" + seconds;
return time
}
alert(militaryTime());
oes
Hello @Polycontrast,
Take a look at the example below...
var date1 = new Date("08/05/2015 23:41:20");
var date2 = new Date("08/06/2015 02:56:32");
var diff = date2.getTime() - date1.getTime();
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
alert(hh + ":" + mm + ":" + ss);
Regards,
Mike
Copy link to clipboard
Copied
Hello @Polycontrast,
Take a look at the example below...
var date1 = new Date("08/05/2015 23:41:20");
var date2 = new Date("08/06/2015 02:56:32");
var diff = date2.getTime() - date1.getTime();
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
alert(hh + ":" + mm + ":" + ss);
Regards,
Mike
Copy link to clipboard
Copied
Many thanks, the code looks good. I will test it to see how it works.
Copy link to clipboard
Copied
I tested the code and it works great, thank you so much!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now