Comparison between two glideduration fields

Alex298
ServiceNow Employee
ServiceNow Employee

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!

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

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

View solution in original post

4 REPLIES 4

Harish KM
Kilo Patron
Kilo Patron

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

Regards
Harish

The SN Nerd
Giga Sage
Giga Sage

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

Alex298
ServiceNow Employee
ServiceNow Employee

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?

Alex298
ServiceNow Employee
ServiceNow Employee

Thanks!