Validation on Date/Time field for user profile timezone On submission
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
Hi all,
I have added date/time field in one of my form in service portal.I want to validate on submission of request.( Due to some restrictions I cant use script include and business rule)
I have set my user profile timezone to NYT.
When I add value in date/time field (2026-03-20 10:15:00) and when I click on submit button on service portal at (2026-03-20 10:17:00). It should validate the date/time with current user profile timezone, if it's expired or past date/time.It should block the submission.
I tried g_user_date_time_format but it is comparing the value in date/time field with the local time(PC time).
How to troubleshoot this scenario.
Thanks in Advance!
Mohankumari
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday - last edited Friday
Hi @Mohankumari
Check this KB :https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0812495
Here you have to use onSubmit() Catalog Client script.
Resolution
To ensure that the date entered in a form is valid upon submission, utilize the onSubmit client script and include a script similar to the following.
Using g_user_date_time_format
function onSubmit() {
var now = new Date();
var dt1 = getDateFromFormat(g_form.getValue("start_date_time"), g_user_date_time_format);
if (dt1 > now) {
g_form.addErrorMessage("Start Date must be before current time.");
return false;
}
}Using g_user_date_format
function onSubmit() {
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
var startDateNum = getDateFromFormat(newValue, g_user_date_format);
if (startDateNum > currentDateNum) {
g_form.showFieldMsg('Start Date must be before current time.', 'error');
return false;
}
}.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
simply use UI policy and no scripting required
the condition in UI policy will take care of the date format and the timezone as well
check this
No Code date validations through (Catalog) UI Policies
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
Hey Ankur,
Yes, UI policy reduce lots of effort on validation. I have applied this approach , but this approach will on onchange. if I enter past date/time , it would validate and show error message.
but if I enter future time , it would accept , but I want to do same kinda validation on OnSubmit.
if the current time is greater than date/time field it will not pass validation here on submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
why you want onSubmit when it can be done with simple UI policy which runs when field changes
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
