Issues with comparing variable dates

Moedeb
Tera Guru

I am trying to do something that I've seen a lot of posts on and I have not been able to get it to work.

 

I have two variables on a catalog item:

  1. u_leave_date
  2. u_return_date

Ultimately I'm just trying to check that the leave date is before the return date and if it isn't, clear the return date value and add a popup message advising the user.

 

This is the code I have in a catalog client script on the catalog item:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var returndate = g_form.getDisplayValue('u_return_date');
    var leavedate = g_form.getDisplayValue('u_leave_date');
    var returndateValue = new Date(returndate).valueOf();
    var leavedateValue = new Date(leavedate).valueOf();

    if (leavedateValue > returndateValue) {
        alert("Return date must be after the departure date");

        g_form.setValue('u_return_date', '');
    }
}

 

Here is the catalog client script:

Moedeb_0-1725429560520.png

 

It is sort of working - but in reality it is just looking at the first part of the date and as long as the first number is higher in the leave date it will give the message and clear the value, if it is lower it just assumes it is correct.

 

IE: (date format dd/mm/yyyyy)

 

Leave date = 04/09/2024

Return date = 03/10/2024

- Because the 03 is before the 04 number wise it will popup advising 

"Return date must be after the departure date
Even though the return date is obviously after the leave date.
 
However in this scenario it doesn't at all object, because the first part of the dates has a higher number ie: 28 is higher than the 04 even though the month or even the year makes it an earlier date.
 

Leave date = 04/09/2024

Return date = 28/05/2023

 

Can someone fix my script please?

1 ACCEPTED SOLUTION

IvanBarsukov
Tera Expert

Hey. Try it that way:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var returndate = getDateFromFormat(g_form.getValue('u_return_date'), g_user_date_format);
    var leavedate = getDateFromFormat(g_form.getValue('u_leave_date'), g_user_date_format);

    var returndateValue = new Date(returndate).valueOf();
    var leavedateValue = new Date(leavedate).valueOf();

    if (leavedateValue > returndateValue) {
        alert("Return date must be after the departure date");

        g_form.setValue('u_return_date', '');
    }
}

View solution in original post

10 REPLIES 10

@Moedeb The script proposed from Ivan with getDateFromFormat should work for your case.

You may also want to have one more script to trigger OnChange on the Leave Date variable. 

 

Cheers,

Tai Vu

@Timi yes I did actually try the UI Policy first and everything looked like it should have worked, but it just didn't do anything.

Here is what I tried (also changed the hours to days and tried 1 hour and 1 day rather than 0 an had the same results - as in nothing happened)

 

Moedeb_0-1725495110053.png

 

Moedeb_1-1725495120969.png

 

 

Hi @Moedeb 

From your UI Policy screenshot, change the condition to more than should to the trick.

Timi_0-1725521319116.png

 

Cheers,

Tai Vu

Neat solution!

IvanBarsukov
Tera Expert

Hey. Try it that way:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var returndate = getDateFromFormat(g_form.getValue('u_return_date'), g_user_date_format);
    var leavedate = getDateFromFormat(g_form.getValue('u_leave_date'), g_user_date_format);

    var returndateValue = new Date(returndate).valueOf();
    var leavedateValue = new Date(leavedate).valueOf();

    if (leavedateValue > returndateValue) {
        alert("Return date must be after the departure date");

        g_form.setValue('u_return_date', '');
    }
}