error in comparing two dates

yoli1
Tera Contributor

hello all my code doesn't seem to work any help?

var currentTime = new GlideDateTime();
var closedtime = new GlideDateTime(current.close_at);
var result = gs.dateDiff(closedtime, currentTime, true);
if ((current.state == '3' || current.state == '4' || current.state == '7') && result > 120) {
    answer = false;
} else {
    answer = true;
}
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@yoli1 

please use GlideDateTime subtract method and ensure field names are correct

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

Anubhav24
Mega Sage
Mega Sage

Hi @yoli1 ,

To calculate the difference you must convert both glidedatetime dates to milliseconds using getnumercivalue and once you have the difference you can implement your logic:

You can do like below:

var currentTime = new GlideDateTime();

var date1 = currenttime.getNumericValue();

Similarly you can convert the closed_at time , you can get the difference in milliseconds and then you can convert the milliseconds into whatever format you need to compare the result in hours / minutes/seconds.

If you want milliseconds in date format you can use setnumericvalue function available.

 

Kindly mark helpful/correct if my response helped you.

SunilKumar_P
Giga Sage

Hi @yoli1, OOB field name of Closed is 'closed_at'. Can you recheck the line 2 in your script?

var closedDatetime = new GlideDateTime(current.close_at);

 

You made need to change it to

var closedDatetime = new GlideDateTime(current.closed_at);

 

Regards,

Sunil