- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 05:12 AM
How to calculate difference between two dates in hours ?
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 07:49 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 05:31 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 05:33 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 05:34 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2017 07:49 AM
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