Catalog variable Date/Time validation – 2 working days (Sun–Thu) with hour‑based lead time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
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 Requirement1. 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)Thank you✅ 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
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');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12m ago
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
2) if you select the non working days (16/5/2026 less 2 working days)
3)if Less than 2 working days ( 18/5/2026)
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 😊
