What am I doing wrong with GlideDate.subtract()?

MikeC2278451151
Tera Contributor

I am trying to simply get the difference between two times fields and put the duration into a duration field. I figured something simple like this would work:

current.duration = GlideDate.subtract(current.start_time, current.end_time);

But I ended having to write this awful thing to get it to work:

var gd1 = new GlideTime(); 
gd1.setDisplayValue(current.start_time.getDisplayValue()); 
var gd2 = new GlideTime(); 
gd2.setDisplayValue(current.end_time.getDisplayValue()); 
 
current.duration = GlideDate.subtract(gd1 ,gd2);

What am I doing wrong here?

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

To execute methods on the GlideDateTime (GlideDate, GlideTime, whatever) API, you must instantiate the values used in the method also as GlideDateTime (...), so something like this will work:

current.duration = GlideDateTime.subtract(new GlideDateTime(current.start_time), new GlideDateTime(current.end_time));

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Try the following script logic:

 

 

 

	var testRec = new GlideRecord('u_test_table');
	testRec.query();
	while (testRec.next()) {
		var sDate = new GlideDateTime(testRec.u_start_date);
		var eDate = new GlideDateTime(testRec.u_end_date);
		var sSecs = sDate.getNumericValue() / 1000;
		var eSecs = eDate.getNumericValue() / 1000;
		var diffSecs = eSecs - sSecs;
//		gs.info('Start:' + sDate + ', End: '+ eDate + ', sSecs: ' + sSecs + ', eSecs' + eSecs + ', diff: ' + diffSecs);
		var epochDate = new GlideDateTime("1970-01-01 00:00:00");
		epochDate.addSeconds(diffSecs);
		var dur = new GlideDateTime(epochDate.getValue());
//		gs.info('dur: ' + dur);
		testRec.u_duration = dur;
		testRec.update();
	}

 

using the info messages I get:

 

*** Script: Start:2025-03-03 21:34:26, End: 2025-03-05 21:34:26, sSecs: 1741037666, eSecs1741210466, diff: 172800
*** Script: dur: 1970-01-03 00:00:00
*** Script: Start:2025-02-14 15:39:48, End: 2025-02-18 16:40:48, sSecs: 1739547588, eSecs1739896848, diff: 349260
*** Script: dur: 1970-01-05 01:01:00

 

On the form, we see:

Screenshot 2025-03-02 184933.png

Brad Bowman
Kilo Patron
Kilo Patron

To execute methods on the GlideDateTime (GlideDate, GlideTime, whatever) API, you must instantiate the values used in the method also as GlideDateTime (...), so something like this will work:

current.duration = GlideDateTime.subtract(new GlideDateTime(current.start_time), new GlideDateTime(current.end_time));

Thank you for the advice. The solution I eventually got to myself was to convert the times to milliseconds, subtract and instantiate a new GlideDuration with that.

 

	var	Duration = current.end_time.dateNumericValue() - current.start_time.dateNumericValue(); 
	current.duration = new GlideDuration(Duration);

Ankur Bawiskar
Tera Patron
Tera Patron

@MikeC2278451151 

I agree with @Brad Bowman here.

You need to use GlideDateTime object and then use them in subtract method

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader