Allow past date in date/time field

Martines
Tera Contributor

Hi community, 

 

I'm struggling with creating the date/time field where only the past dates are allowed.  I found this code. 

Martines_0-1770898724144.png

But it doesn't work. The accepted behavior is that user is not able to choose from the date/time component the future dates. (This would be happy case) Or at least when user choose date in future he will get the message that future dates are not allowed. 

 

Do you have any suggestions? Thank you in advance. 

9 REPLIES 9

Ankur Bawiskar
Tera Patron

@Martines 

this should work fine

AnkurBawiskar_0-1770899734838.png

AnkurBawiskar_1-1770899800825.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Martines 

Did my approach work for you?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hemanth EK
Tera Contributor

Create a Client Script:

Type is Onchange and field is start_date_time

Screenshot 2026-02-12 181349.png

 

How It works

  • Gets selected date

  • Compares with current date/time

 

 

Martines
Tera Contributor

I did it this way: 

 

Created UI policy: 

Martines_0-1770903671745.png

Where I include every field that needs to be checked. 

Than in script I made the if statements: 

Martines_2-1770903740582.png

I know that it can be simplified with for loop 😄 

 

@Martines 

Your script won't help user to know which field actually was having invalid value.

If Start date is invalid then it will show error message besides all fields.

You will require separate UI policies for each field

Logic I already shared

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader