How to calculate difference between two dates in hours ?

coolsaurabh
Tera Contributor

How to calculate difference between two dates in hours ?

1 ACCEPTED SOLUTION

markus_schaer
Tera Guru

Hi Sourabh



Here is an example on how to get the difference between two GlideDateTime objects and convert the result to hours:



var start = new GlideDateTime("2016-08-28 09:00:00");


var end = new GlideDateTime("2016-08-31 08:10:00");



// Duration in hours


var dur_seconds = gs.dateDiff(start, end, true); // returns the number of seconds as String


var dur_hours = Math.round(dur_seconds / 3600); // Math.round() is optional and will round the number to the nearest integer


gs.print(dur_hours); // *** Script: 71



This is a second example with the dedicated class GlideDuration. This class does not have a function to calculate hours (only days), but it can be useful to work with durations in general:



var start = new GlideDateTime("2016-08-28 09:00:00");


var end = new GlideDateTime("2016-08-31 08:10:00");



// Duration in days


var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object


gs.print(dur.getDisplayValue()); // *** Script: 2 Days 23 Hours 10 Minutes


gs.print(dur.getDayPart()); // *** Script: 2


gs.print(dur.getRoundedDayPart()); // *** Script: 3



Best regards


Markus


View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sourabh,



When you use dateDiff() method it has a third boolean parameter as true/false. True means give difference in seconds and false means give difference in duration i.e days hours min seconds



Use split method and the first position of the split array should give you the days



var date1 = new GlideDateTime();


var date2 = new GlideDateTime();


date1.setDisplayValueInternal('2014-01-01 12:00:00');


date2.setDisplayValueInternal('2014-01-03 13:00:00');



// Determine the difference as number of seconds (returns a string)


// Use getDisplayValue() to convert the string to the format expected by dateDiff()


var diff = gs.dateDiff(date1.getDisplayValue(), date2.getDisplayValue(), false);



var days = diff.split(" ")[0];


gs.print(days);



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Harsh Vardhan
Giga Patron

Hi Sourabh,



check the below code.



getTimeDiff();  


 


  function getTimeDiff(){  


  var startDate = current.u_start_date.getGlideObject();  


  var endDate = current.u_end_date.getGlideObject();  


 


  current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  


 


 


  }



Thanks,


Harshvardhan


Harish KM
Kilo Patron
Kilo Patron

markus_schaer
Tera Guru

Hi Sourabh



Here is an example on how to get the difference between two GlideDateTime objects and convert the result to hours:



var start = new GlideDateTime("2016-08-28 09:00:00");


var end = new GlideDateTime("2016-08-31 08:10:00");



// Duration in hours


var dur_seconds = gs.dateDiff(start, end, true); // returns the number of seconds as String


var dur_hours = Math.round(dur_seconds / 3600); // Math.round() is optional and will round the number to the nearest integer


gs.print(dur_hours); // *** Script: 71



This is a second example with the dedicated class GlideDuration. This class does not have a function to calculate hours (only days), but it can be useful to work with durations in general:



var start = new GlideDateTime("2016-08-28 09:00:00");


var end = new GlideDateTime("2016-08-31 08:10:00");



// Duration in days


var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object


gs.print(dur.getDisplayValue()); // *** Script: 2 Days 23 Hours 10 Minutes


gs.print(dur.getDayPart()); // *** Script: 2


gs.print(dur.getRoundedDayPart()); // *** Script: 3



Best regards


Markus