Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to convert difference of days into hh:mm:ss format in glidedatetime();

Hemakumar J
Tera Contributor
Consider the following code
 
var pastDate = new GlideDateTime("2020-04-16 20:00:00");
var currDate = new GlideDateTime("2020-04-17 20:30:55");
var dur1 = GlideDateTime.subtract(pastDate,currDate); 
 
My expectation is since the difference is 24 hours plus 30 mins I need the output in HH:mm:ss format . i.e 24:30:00
Any help here would be appreciated. I am attaching few more methods which I have already tried.
 
 
 
 
 
gs.print(dur1);
Here this is printing
1970-01-02 00:30:55
gs.print(dur1.getDisplayValue);
this is printing 
1 Day 30 Minutes
dur.getByFormat('HH:mm:ss');
this is printing ignoring the day of difference 
 
00:30:55
 
gs.print(dur1.getDurationValue());
 
1 00:30:55
2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

There's probably a more elegant way of doing this, but here is a way to get your desired results:

 

var pastDate = new GlideDateTime("2020-04-16 20:00:00");
var currDate = new GlideDateTime("2020-04-17 21:30:55");
var dur1 = GlideDateTime.subtract(pastDate,currDate);
var hrsDiff = Math.floor((currDate.getNumericValue() - pastDate.getNumericValue()) / (60 * 60 * 1000));
gs.print(hrsDiff + ':' + dur1.getByFormat('mm:ss'));

 

 

View solution in original post

_Tushar Soni
Kilo Sage
Kilo Sage

Hello @Hemakumar J ,

// Define the two dates
var pastDate = new GlideDateTime("2020-04-16 20:00:00");
var currDate = new GlideDateTime("2020-04-17 20:30:55");

// Calculate the difference in milliseconds
var diffInMilliseconds = currDate.getNumericValue() - pastDate.getNumericValue();

// Convert milliseconds to seconds
var diffInSeconds = Math.floor(diffInMilliseconds / 1000);

// Convert seconds to hours, minutes, and seconds
var hours = Math.floor(diffInSeconds / 3600);
var minutes = Math.floor((diffInSeconds % 3600) / 60);
var seconds = diffInSeconds % 60;

// Format as HH:mm:ss
var formattedDuration = (hours < 10 ? '0' : '') + hours + ':' +
                        (minutes < 10 ? '0' : '') + minutes + ':' +
                        (seconds < 10 ? '0' : '') + seconds;

// Output the formatted duration
gs.info(formattedDuration);

 

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!

View solution in original post

5 REPLIES 5

_Tushar Soni
Kilo Sage
Kilo Sage

Hello @Hemakumar J ,

// Define the two dates
var pastDate = new GlideDateTime("2020-04-16 20:00:00");
var currDate = new GlideDateTime("2020-04-17 20:30:55");

// Calculate the difference in milliseconds
var diffInMilliseconds = currDate.getNumericValue() - pastDate.getNumericValue();

// Convert milliseconds to seconds
var diffInSeconds = Math.floor(diffInMilliseconds / 1000);

// Convert seconds to hours, minutes, and seconds
var hours = Math.floor(diffInSeconds / 3600);
var minutes = Math.floor((diffInSeconds % 3600) / 60);
var seconds = diffInSeconds % 60;

// Format as HH:mm:ss
var formattedDuration = (hours < 10 ? '0' : '') + hours + ':' +
                        (minutes < 10 ? '0' : '') + minutes + ':' +
                        (seconds < 10 ? '0' : '') + seconds;

// Output the formatted duration
gs.info(formattedDuration);

 

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!