Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Timestamp calculation

Engaged ,
Dec 04, 2022 Dec 04, 2022

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

 

TOPICS
Actions and scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Dec 04, 2022 Dec 04, 2022

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

Translate
Adobe
Advisor ,
Dec 04, 2022 Dec 04, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 04, 2022 Dec 04, 2022

Many thanks, the code looks good. I will test it to see how it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 06, 2022 Dec 06, 2022
LATEST

I tested the code and it works great, thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines