Catalog Client Script Date today
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 09:14 AM
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.
Here is the client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 09:44 AM
Hello @Michael Galang ,
I would recommend implementing this check using a Catalog UI Policy. It's much easier.
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 09:58 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 10:06 AM - edited 05-26-2025 10:07 AM
Thank you for all your reponse. I already fix the issue using below script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 10:41 AM
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:
So I really recommend that you implement date validation using Catalog UI Policies and avoid all these pitfalls and reduce the complexity.
Regards,
Robert