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

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.

Hi,

As Allen replied, the original code is as follows and the question was about this not working correctly. Can it be that the field type was Date and it was changed to Date/Time?

    var due = getDateFromFormat(due1, g_user_date_time_format);

Hi,

Glad you found my reply as Helpful.

Thanks


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