Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Comparing two Glide Date Times (less than or greater than)

JJG
Kilo Guru

I am trying to compare 2 Glide Date Times via script include:

        var answer;
        var dueDate = this.getParameter("sysparm_time");
        gs.info("dueDate: " + dueDate);

        var nowPlus2Hours = new GlideDateTime(gs.nowDateTime());
        var hours = (60 * 60) * 2;
        nowPlus2Hours.addSeconds(hours);
        gs.info("nowPlus2Hours: " + nowPlus2Hours);


        if (dueDate.getDisplayValue() < nowPlus2Hours.getDisplayValue()) {

            answer = false;

        } else {

            answer = true;

        }

        return answer;

 

As you can see, if the due date is less than nowPlus2Hours, it should return false. As it is currently, the script include is always returning true.

 

Output in the logs: (As you can see, the dueDate is less than nowPlus2Hours, which means the script should return false. Instead, it is returning true)

nowPlus2Hours: 2022-02-24 13:12:30

dueDate: 2022-02-24 06:00:00 AM

1 ACCEPTED SOLUTION

I was able to run a check to see if the due date was AM or PM. If it was PM, I added 12 hours to the dueDate variable. It worked after that.

 

var answer;
var dueDate = new GlideDateTime(this.getParameter("sysparm_time"));

var AMorPM = dueDate.indexOf("PM");
if (AMorPM != -1) {
    var add12Hours = (60 * 60) * 12;
    dueDate.addSeconds(add12Hours);
}
var nowPlus2Hours = new GlideDateTime(gs.nowDateTime());
var hours = (60 * 60) * 2;
nowPlus2Hours.addSeconds(hours);
if (dueDate.compareTo(nowPlus2Hours) != -1) {

    answer = "true";

} else {

     answer = "false";

}

return answer;

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Hi @JJG 

Methid getDisplayValue() returns a String and that is not comparable.

Instead use getNumericValue() which returns a numeric value:

if (dueDate.getNumericValue() < nowPlus2Hours.getNumericValue()) {

}

Kind regards
Maik

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

this should work fine

var answer;
var dueDate = new GlideDateTime(this.getParameter("sysparm_time")); // initialize and set value

var nowPlus2Hours = new GlideDateTime(); // no need to pass now time
var hours = (60 * 60) * 2;
nowPlus2Hours.addSeconds(hours);

if (dueDate < nowPlus2Hours) {
	answer = false;
} else {
	answer = true;
}

return answer;

Regards
Ankur

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

Unfortunately, this is not working. It looks like it is not converting the due date to 24 hour format.

 

var answer;
dueDate = new GlideDateTime(this.getParameter("sysparm_time"));

var nowPlus2Hours = new GlideDateTime(); // no need to pass now time
var hours = (60 * 60) * 2;
nowPlus2Hours.addSeconds(hours);

gs.info("dueDate: " + dueDate);
gs.info("nowPlus2Hours: " + nowPlus2Hours);

   if (dueDate < nowPlus2Hours) {
            answer = false;
    } else {
            answer = true;
    }

return answer;

 

Here is the output in the logs: 

dueDate: 2022-03-01 04:51:22  <--- This is PM, so it should be 16:51

nowPlus2Hours: 2022-03-01 15:55:50

Hi,

the script is comparing time in GMT so it should work fine

Regards
Ankur

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