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