How to restrict start date to be selected as past date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 08:44 PM
I have created below mentioned onchange client script, this is working that i am not able to select the past date but along with past date its not even allowing me to select the today's date also and giving me the same error. please suggest
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var date1 = new Date(g_form.getValue('start_date'));
var date2 = new Date();
if(date1 <date2){
alert("Add future day");
g_form.clearValue('start_date');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 09:07 PM
use this
if(date1 <= date2){
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
‎04-14-2025 09:12 PM
Hello @VIKAS MISHRA ,
Please use the below OnChange Client script,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 10:21 PM - edited ‎04-14-2025 10:46 PM
Hello @VIKAS MISHRA ,
Please use this script if you are using a Date (no time) field:
var startDate = getDateFromFormat(
g_form.getValue('start_date'), g_user_date_format),
today = new Date().setHours(0, 0, 0, 0);
if (startDate < today) {
g_form.clearValue('start_date');
g_form.showErrorBox('start_date', 'Start date cannot be in the past');
}
Use this if you are using a Date/Time field:
var startDate = getDateFromFormat(
g_form.getValue('start_date'), g_user_date_time_format),
now = new Date().getTime();
if (startDate < now) {
g_form.clearValue('start_date');
g_form.showErrorBox('start_date', 'Start date cannot be in the past');
}
Or create a UI Policy and UI Policy Action as shown below:
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 10:32 PM
Not sure what the exact case is here, but did you try using a UI Policy to restrict the user from selecting a past date? UI Policies are low-code and easy to configure.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************