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 validation if date is 7 days from today

shaik23
Tera Expert

Hello mates,

 

Below is my requirement.

 

Create a catalog client script that runs when the ‘Is this due to be returned at a specific date?’ field is updated.

Generate an error if the Date is not 7 days from today

 

 

for this i have created a catalog client script script given below but its not working as expected, i am missing logic can anyone please help me.

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

    // Calculate the valid date (7 days from today)
    var validDate = new Date(today);
    validDate.setDate(today.getDate() + 7);

    // Get selected date
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0, 0, 0, 0);

    // Compare the dates directly (Date vs Date)
    if (selectedDate.getTime() !== validDate.getTime()) {
        g_form.showFieldMsg('is_this_due_to_be_returned_at_a_specific_date',
            'Date must be exactly 7 days from today', 'error');
    } else {
        g_form.hideFieldMsg('is_this_due_to_be_returned_at_a_specific_date', true);
    }



}

 

shaik23_0-1763043826348.png

shaik23_1-1763043835124.png
Date format is month/date/year in our instance

8 REPLIES 8

@shaik23 

what's your business requirement?

date selected should be Exact 7 days from now?

try this

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

    var dateMS = 7 * 24 * 60 * 60 * 1000; // 7 days
    var selectedValue = g_form.getValue('is_this_due_to_be_returned_at_a_specific_date');
    var todayDate = new Date();
    todayDate.setTime(dateMS);

    var selectedDt = new Date(getDateFromFormat(selectedValue, g_user_date_format));

    if (selectedDate.getTime() !== todayDate.getTime()) {
        g_form.showFieldMsg('is_this_due_to_be_returned_at_a_specific_date',
            'Date must be exactly 7 days from today', 'error');
    } else {
        g_form.hideFieldMsg('is_this_due_to_be_returned_at_a_specific_date', true);
    }

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@shaik23 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

ChallaR
Giga Guru

HI @shaik23 ,

 

i have corrected few lines , can you please try below script -

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

    // Parse selected date (MM/DD/YYYY)
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0, 0, 0, 0);

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

    // Calculate difference in days
    var diffInMs = selectedDate.getTime() - today.getTime();
    var diffInDays = diffInMs / (1000 * 60 * 60 * 24);

    if (diffInDays !== 7) {
        g_form.showFieldMsg('is_this_due_to_be_returned_at_a_specific_date',
            'Date must be exactly 7 days from today', 'error');
    } else {
        g_form.hideFieldMsg('is_this_due_to_be_returned_at_a_specific_date', true);
    }
}

 

Thanks,

Rithika.ch

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @shaik23 

Try this client script, if you only need to do it via client script

function onChange(control, oldValue, newValue, isLoading) {

    // 1. Get the variable name (replace with your actual variable name)
    var dateField = 'is_this_due_to_be_returned_at_a_specific_date';

    // 2. Don't run on load, and clear messages if the field is emptied
    if (isLoading || newValue == '') {
        g_form.hideFieldMsg(dateField, true);
        return;
    }

    // --- Start Validation ---

    // 3. Get today's date (local time) and set to midnight
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // 4. Calculate the only valid date (7 days from now, local time)
    var validDate = new Date();
    validDate.setDate(today.getDate() + 7);
    validDate.setHours(0, 0, 0, 0);

    // 5. Get the selected date. 
    // We parse 'yyyy-MM-dd' manually to create a LOCAL date.
    // This avoids all UTC/timezone problems.
    var parts = newValue.split('-'); // [yyyy, MM, dd]
    // Note: Month is 0-indexed (0=Jan, 11=Dec)
    var selectedDate = new Date(parts[0], parts[1] - 1, parts[2]);

    // 6. Compare the dates
    if (selectedDate.getTime() !== validDate.getTime()) {
        // Not the right date, show an error
        g_form.showFieldMsg(dateField, 'Date must be exactly 7 days from today', 'error');
    } else {
        // It's the correct date, hide any error messages
        g_form.hideFieldMsg(dateField, true);
    }
}



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma