Catalog Client Script Date today

Michael Galang
Tera Contributor

Dear Experts, 

I have a catalog client script that is not working. Everytime I select the date today, there is an error, 'The selected date cannot be in the past' and the selected date value always shows the date yesterday when I get the display value.  It should accept the date today regardless of the time and there should be no error message.  I would appreciate your help regarding this matter. 

MichaelGalang_1-1748273377173.png

Here is the client script: 

 

 

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

    var today = new Date();
    today.setHours(0, 0, 0, 0);

    var maxDate = new Date(today);
    maxDate.setDate(today.getDate() + 28);
    maxDate.setHours(0, 0, 0, 0);

    var selected_dateValue = new Date(newValue);
    selected_dateValue.setHours(0, 0, 0, 0);
    g_form.addInfoMessage('The selected_dateValue is: ' + selected_dateValue);
    g_form.addInfoMessage('The today time is: ' + today);


    if (selected_dateValue === today) {
        return;
    } else if (selected_dateValue < today) {
        g_form.setValue('target_upgrade_date', '');
        g_form.addErrorMessage('The selected date cannot be in the past.');
    } else if (selected_dateValue > maxDate) {
        g_form.setValue('target_upgrade_date', '');
        g_form.addErrorMessage('The selected date cannot be more than 4 weeks from today.');
    }
}
5 REPLIES 5

Robert H
Mega Sage

Hello @Michael Galang ,

 

I would recommend implementing this check using a Catalog UI Policy. It's much easier.

 

RobertH_0-1748277856597.png

 

Regards,

Robert

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael Galang 

since you already create client script, here is the updated one

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

    // Parse the selected date and normalize to midnight
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0, 0, 0, 0);

    // Today's date at midnight
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // Max allowed date (28 days from today) at midnight
    var maxDate = new Date();
    maxDate.setHours(0, 0, 0, 0);
    maxDate.setDate(maxDate.getDate() + 28);

    if (selectedDate < today) {
        g_form.addErrorMessage('The selected date cannot be in the past.');
        g_form.clearValue('target_upgrade_date');
    } else if (selectedDate > maxDate) {
        g_form.addErrorMessage('The selected date cannot be more than 28 days from today.');
        g_form.clearValue('target_upgrade_date');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Michael Galang
Tera Contributor

Thank you for all your reponse. I already fix the issue using below script: 

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

    var today = new Date();
    today.setHours(0, 0, 0, 0);

    var maxDate = new Date(today);
    maxDate.setDate(today.getDate() + 29);
    maxDate.setHours(0, 0, 0, 0);

    //get Selected date value from target_upgrade_date field
    var selected_dateValue = new Date(newValue);
    var selected_dateValue1 = selected_dateValue;
    selected_dateValue1.setDate(selected_dateValue.getDate() + 1);

    // Compare dates
    if (selected_dateValue1 === today) {
        return;
    } else if (selected_dateValue1 < today) {
        g_form.setValue('target_upgrade_date', '');
        g_form.addErrorMessage('The selected date cannot be in the past.');
    } else if (selected_dateValue1 > maxDate) {
        g_form.setValue('target_upgrade_date', '');
        g_form.addErrorMessage('The selected date cannot be more than 4 weeks from today.');
    }
}

function formatDateToYMD(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0');
    var day = String(date.getDate()).padStart(2, '0');
    return year + '-' + month + '-' + day;
}

Hello @Michael Galang ,

 

Good to hear that you got it working for now but please note that this will not work when the form is submitted by a user who does not use the "YYYY-MM-DD" date format:

RobertH_0-1748281184699.png

 

So I really recommend that you implement date validation using Catalog UI Policies and avoid all these pitfalls and reduce the complexity.

 

Regards,

Robert