Restricting past date and time in date/time field using onChange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 01:52 AM
Restricting past date and time in date/time field using on change client script as below, but it is restricting only old date IF I am selecting past time of todays date it is not restricting, it so it should restrict past time too
my onChange client script for DateTime field -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var EventDateTime = newValue;
var newdt = new Date(EventDateTime);
var EventDate = formatDate(newdt, 'yyyyMMdd');
var RightNow = new Date();
var RightNowDate = formatDate(RightNow, 'yyyyMMdd');
if (EventDate < RightNowDate) {
g_form.clearValue('end_date');
g_form.showFieldMsg('end_date', 'Planned end date must be after the current date', 'error', true); }}
@Ankur Bawiskar please assist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 03:00 AM
why are you selecting today's date?
share your form screenshots.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 03:21 AM
@Ankur Bawiskar User must can select future time also even it is for todays date that is the customer requirement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 03:24 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideErrorBox('end_date');
if (newValue != oldValue) {
var RightNow = new Date().getTime();
var end = new Date(getDateFromFormat(g_form.getValue('end_date'), g_user_date_time_format));
if (end <= RightNow) {
g_form.clearValue('end_date');
g_form.showFieldMsg('end_date', 'Planned end date must be after the current date', 'error', true);
}
}
}
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
10-26-2023 03:31 AM
Hi,
Why don't you use a No Code Solution like below using conditions on UI policy
In the Run script you can throw an error or info message and return false
More Eg here ->No Code date validations through (Catalog) UI Poli... - ServiceNow Community

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 03:45 AM
Hello @fgh1 ,
You can use the UI policy for date validations.
Refer to this article by @Mark Roethof.
Thanks
Anand