Compare a Date Field with the Current Servicenow Date.

crhistopherjuar
Kilo Expert

Hi Experts,

I am trying to Compare a Date Field with the Current Servicenow Date.

The Date field is called "u_end_date" and it is just a Date Field, not a Date Time Field.

So right now I am just trying to compare the Date field with the Current Servicenow Date to send a message, if these dates are the same just send an info message that the dates are the same. I did a business rule but is not working.

This is the business rule code:

var now = gs.now();

var end = current.u_end_date;

if (now == end){

gs.addInfoMessage( now + "=" + end);

}

I did a test before, with out the if condition to see the values of both and I see that the order of the values are different.

find_real_file.png

I think my code is not working because the order of the values.

Do you have any Idea why my code is not working?

6 REPLIES 6

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

That is strange. they should be the same. here I created a date field my self (u_test)


find_real_file.png


And if I change the date for tomorrow on the field:



find_real_file.png



If you test creating a new date field, do you get the same format on that one?



/Göran


dvp
Mega Sage
Mega Sage

The only way I have seen date comparison works is by using glide datetime.



Try this



var now = new GlideDateTime();
var end = new GlideDateTime(current.u_end_date);


if(now.getDate() == end.getDate()){
  gs.addInfoMessage( now + "=" + end);
}


Hi dvp,



I tested your code and still not showing the message.



Actually before this code that I am sharing here, I tried to do with GlideDateTime() and tried to get de date by getDate() but when I tested I realize that the "now" variable appears in the info message as null.


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

That wont work.. putting only a date "2017-03-03" into GlideDateTime will reslut in "2017-03-03 00:00:00" meaning if you don't do the comparing exactly at this time it won't match the the "new GlideDateTime".



//Göran