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.

Date comparison is not working as expected

Digit
Kilo Guru

I would LOVE a second set of eyes on my code. I am trying to write an onSubmit client script that compares my due_date field to yesterday (24 hours ago). I have converted both to milliseconds, and the values appear to be correct. Then if due_date < yesterday, I want to display an error message and prevent the user from saving.

If I enter a date that is less than yesterday, and submit, I get the error message. If I then immediately enter yesterday's date and submit, I still get the error. 

Here's the code:

function onSubmit() {

    var now = new Date();
    var yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    var y = yesterday.getTime();

    var due1 = g_form.getValue('due_date');
    var due = getDateFromFormat(due1, g_user_date_format);

    if (due < y) {
        g_form.addErrorMessage('The Planned Review Date should be no earlier than yesterday.');
        return false;

    } 
    else g_form.addInfoMessage('Passed.');

}

Thanks a ton for any assistance!!!

1 ACCEPTED SOLUTION

Yes, that was my original statement. Thanks for pointing that out! 

I changed it so much that I missed the actual fix, which was flipping the formats for yesterday and due:

var yesterday_str = formatDate(yesterday, g_user_date_time_format);
var due = g_form.getValue("due_date");

Changing each field's format is what did the trick and actually allowed the comparison to function correctly.

View solution in original post

7 REPLIES 7

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Digit

The problem is with "g_user_date_time_format". Field "due_date" is of type "Date" so it should be "g_user_date_format".

function onSubmit() {
    var now = new Date();
    var yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    var y = yesterday.getTime();

    var due1 = g_form.getValue('due_date');
    var due = getDateFromFormat(due1, g_user_date_format);

    if (due < y) {
        g_form.addErrorMessage('The Planned Review Date should be no earlier than yesterday.');
        return false;

    } else {
        g_form.addInfoMessage('Passed.');
    }
}

Thanks for the feedback, but "due_date" is actually a date/time field.

I tried changing it to 

var due = getDateFromFormat(due1, g_user_date_format);

in order to see if it made any difference, and unfortunately I got the same results. 

Both final fields produce a numeric value in milliseconds, but for some reason, they never pass, regardless of what date I try.

Digit
Kilo Guru

Just discovered the answer, for any future folks looking for assistance on a related issue. 

I changed this original line:

var due = getDateFromFormat(due1, g_user_date_format);

to:

var due = getDateFromFormat(due1, g_user_date_time_format);

and everything then worked correctly.

Hi,

I may be missing something, but you already had that line in your original post?


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!