- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2017 03:20 AM
Hi Markus,
Thanks a lot for your response. It providing time in seconds in proper way. But in hours it is not calculating properly.
- var dur_seconds = gs.dateDiff(start, end, true); // returns a String
- var dur_hours = Math.round(dur_seconds / 60 / 60);
- gs.print(dur_hours);
In above part if dur_seconds = 2400 second
then dur_hours = 1
while it should provide 2400/60/60 = 2/3 = .66
Please help.
Thanks,
Sourabh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2017 04:33 AM
Try changing "(dur_seconds / 60 / 60)" to "(dur_seconds / 3600)"
Also, if you're doing "Math.round()" then .66 rounded = 1.