- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2018 10:49 PM
var duration_1 = new Glideduration('3 12:00:00'); // 3 days 12 hours var duration_2 = new Glideduration('12:00:00'); // 12 hours
Is there a way to compare that two duration? (eg: return false/true when duration_1 is longer than duration_2)
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2018 11:05 PM
Something like this
var duration_1 = new GlideDuration('3 12:00:00'); // 3 days 12 hours
var duration_2 = new GlideDuration('12:00:00'); // 12 hours
function compareDurations(duration_1, duration_2) {
var dt1 = new GlideDateTime(duration_1.getValue());
var dt2 = new GlideDateTime(duration_2.getValue());
return dt1.compareTo(dt2);
}
var compDurationValue = compareDurations(duration_1, duration_2);
switch(compDurationValue) {
case 1:
gs.addInfoMessage("Duration_1 is larger than Duration_2");
break;
case -1:
gs.addInfoMessage("Duration_2 is larger than Duration_1");
break;
default:
gs.addInfoMessage("Durations are the same");
break;
}
Output
Duration_1 is larger than Duration_2 |
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2018 10:54 PM
Example here
var gdt = new GlideDateTime("2011-08-31 08:00:00");
var gtime1 = new GlideTime();
gtime1.setValue("00:00:20");
gdt.subtract(gtime1);
var gtime2 = gdt.getTime();
gs.info(gtime2.getByFormat('hh:mm:ss'));
Refrer this
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDateTimeSubtract_GlideTime
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDateSubtract_GlideDate_GlideDate
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2018 11:05 PM
Something like this
var duration_1 = new GlideDuration('3 12:00:00'); // 3 days 12 hours
var duration_2 = new GlideDuration('12:00:00'); // 12 hours
function compareDurations(duration_1, duration_2) {
var dt1 = new GlideDateTime(duration_1.getValue());
var dt2 = new GlideDateTime(duration_2.getValue());
return dt1.compareTo(dt2);
}
var compDurationValue = compareDurations(duration_1, duration_2);
switch(compDurationValue) {
case 1:
gs.addInfoMessage("Duration_1 is larger than Duration_2");
break;
case -1:
gs.addInfoMessage("Duration_2 is larger than Duration_1");
break;
default:
gs.addInfoMessage("Durations are the same");
break;
}
Output
Duration_1 is larger than Duration_2 |
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2018 12:42 AM
Thanks for the answer.
I am trying to use this in a scoped application and getValue() can only be used in the global scope. Is there any way to get around it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2018 01:12 AM
Thanks!