Catalog variable Date/Time validation – 2 working days (Sun–Thu) with hour‑based lead time

Sirri
Tera Guru

Hi Community,

I’m working on a Service Catalog Date/Time variable validation and would like confirmation or suggestions for improvement.

Variable Details

  • Variable name: time_to_send_out_comms
  • Variable type: Date/Time
  • Used on: Service Request (SR)

    Business Requirement

    1. Past Date/Time Validation

    • The selected date/time must not be in the past

    If user selects a past date/time, show the error message:

    “The time chosen to send out the communication occurs in the past, kindly select a future date/time to proceed.”

    2. Minimum Lead Time – 2 Working Days (Hour‑Based)

    • The selected date/time must be at least 2 working days after the current date/time
    • Working days: Sunday to Thursday
      (Friday and Saturday are non‑working days)
    • Lead time must be hour‑based, not just date‑based

    Example

    Current date/time:

    13/May/2026 13:05:00 (Wednesday)

    Valid selections

    • 17/May/2026 13:06:00
    • 17/May/2026 15:00:00
    • Any date/time after the same HH:mm:ss on the 2nd working day

    Invalid selections

    • 17/May/2026 13:04:00
    • Any date/time before 17/May/2026 13:05:00

    If user does not allow 2 working days lead time, show the error message:

    “Please allow two working days lead time for approvals and content review.”
    Please provide with exact code so it will helpful to me.

    Thank you

     





17 REPLIES 17

Tejas Adhalrao
Kilo Sage

Hi @Sirri  ,

You can achieve this requirement using an onChange Catalog Client Script on the Date/Time variable time_to_send_out_comms.

this script handles :

 

  • Past date/time validation
  • 2 working days lead time validation
  • Sunday–Thursday working week
  • Hour/minute/second level validation
function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || !newValue) {
        return;
    }

    g_form.hideFieldMsg('time_to_send_out_comms', true);

    // Selected date/time
    var selectedDateTime = new GlideDateTime(newValue);

    // Current date/time
    var currentDateTime = new GlideDateTime();

    // ----------------------------------------------------------------
    // 1. Past Date/Time Validation
    // ----------------------------------------------------------------

    if (selectedDateTime.before(currentDateTime)) {

        g_form.showFieldMsg(
            'time_to_send_out_comms',
            'The time chosen to send out the communication occurs in the past, kindly select a future date/time to proceed.',
            'error'
        );

        g_form.clearValue('time_to_send_out_comms');
        return;
    }

    // ----------------------------------------------------------------
    // 2. Minimum 2 Working Days Lead Time Validation
    // Working Days = Sunday to Thursday
    // Friday & Saturday are non-working days
    // ----------------------------------------------------------------

    var minimumAllowedDateTime = new GlideDateTime(currentDateTime);

    var workingDaysCount = 0;

    while (workingDaysCount < 2) {

        // Add one calendar day
        minimumAllowedDateTime.addDaysLocalTime(1);

        // Day of week
        // 1 = Monday
        // 2 = Tuesday
        // 3 = Wednesday
        // 4 = Thursday
        // 5 = Friday
        // 6 = Saturday
        // 7 = Sunday

        var dayOfWeek = parseInt(
            minimumAllowedDateTime.getDayOfWeekLocalTime(),
            10
        );

        // Valid working days: Sunday to Thursday
        if ((dayOfWeek >= 1 && dayOfWeek <= 4) || dayOfWeek == 7) {
            workingDaysCount++;
        }
    }

    // Final validation
    if (selectedDateTime.before(minimumAllowedDateTime)) {

        g_form.showFieldMsg(
            'time_to_send_out_comms',
            'Please allow two working days lead time for approvals and content review.',
            'error'
        );

        g_form.clearValue('time_to_send_out_comms');
    }
}

 

 

 

@Tejas Adhalrao ,

Thank you for your response.

Could you please confirm if you have tested this at your end and whether it is working as expected? I have tried implementing the code you provided, but it is not working in my case.

I am encountering an error, and I have attached a screenshot that includes both the issue and the console error for your reference. Kindly review the attachment and advise.


Script:

g_form.hideFieldMsg('time_to_send_out_comms');

// Selected date/time
var selectedDateTime = new GlideDateTime();
selectedDateTime.setDisplayValue(newValue);

// Current date/time
var currentDateTime = new GlideDateTime();

// ------------------------------
// 1. Past Date Validation
// ------------------------------
if (selectedDateTime.before(currentDateTime)) {

g_form.showFieldMsg(
'time_to_send_out_comms',
'The selected time is in the past. Please choose a future date/time.',
'error'
);

g_form.clearValue('time_to_send_out_comms');
return;
}

// ------------------------------
// 2. 2 Working Days Validation
// ------------------------------
var minimumAllowedDateTime = new GlideDateTime(currentDateTime);
var workingDaysCount = 0;

while (workingDaysCount < 2) {

minimumAllowedDateTime.addDaysLocalTime(1);

var dayOfWeek = minimumAllowedDateTime.getDayOfWeekLocalTime();

// Sunday–Thursday
if (dayOfWeek >= 1 && dayOfWeek <= 5) {
workingDaysCount++;
}
}

// Final validation
if (selectedDateTime.before(minimumAllowedDateTime)) {

g_form.showFieldMsg(
'time_to_send_out_comms',
'Please allow a minimum of 2 working days.',
'error'
);

g_form.clearValue('time_to_send_out_comms');
}


Sirri_0-1778841233668.png

 

Tejas Adhalrao
Kilo Sage

Hi @Sirri  , here is update script with screenshorts 

on change client script  (reoplace your variable name)

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

    // Hide any previous error messages on this field
    g_form.hideFieldMsg('select_date_time', true);

    // 1. Get Selected Date (Convert ServiceNow format to JS Date)
    var selectedDateStr = g_form.getValue('select_date_time');
    var selectedDate = new Date(getDateFromFormat(selectedDateStr, g_user_date_time_format));
    var now = new Date();

    // ----------------------------------------------------------------
    // 2. Past Date/Time Validation
    // ----------------------------------------------------------------
    if (selectedDate <= now) {
        g_form.addErrorMessage('The time chosen occurs in the past. Please select a future date/time.' );
        g_form.clearValue('select_date_time');
        return;
    }

    // ----------------------------------------------------------------
    // 3. Minimum 2 Working Days (Sun-Thu) Lead Time Validation
    // ----------------------------------------------------------------
    var minAllowedDate = new Date(now.getTime());
    var workingDaysAdded = 0;

    // Loop until we have added 2 working days
    while (workingDaysAdded < 2) {
        minAllowedDate.setDate(minAllowedDate.getDate() + 1);
        
        var dayOfWeek = minAllowedDate.getDay(); 
        // JS Date getDay(): 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat
        
        // Working days: Sunday(0) through Thursday(4)
        if (dayOfWeek >= 0 && dayOfWeek <= 4) {
            workingDaysAdded++;
        }
    }

    // Final Comparison
    if (selectedDate < minAllowedDate) {
        g_form.addErrorMessage('Please allow at least two working days (Sun-Thu) lead time for approvals.');
        g_form.clearValue('select_date_time');
    }
}

 Test Case :

 today date  - 15/5/2026

 

1) if you select the past date 14/5/2026 shows error

TejasAdhalrao_0-1778843915696.png

 

2) if you select the non working days (16/5/2026 less 2 working days)

TejasAdhalrao_1-1778844034295.png

 

3)if Less than 2 working days ( 18/5/2026)

TejasAdhalrao_2-1778844122308.png

 

 

 

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

thanks ,

tejas 😊