Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Validation on Date/Time field for user profile timezone On submission

Mohankumari
Kilo Explorer

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 

2 REPLIES 2

Tanushree Maiti
Tera Sage

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;
}
}

 .

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@Mohankumari 

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! 🙏

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