- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 07:25 AM
Hello everyone,
I'm working on script to validate date/time field, which one the user shouldn't be able to enter a past date/time.
// Client script:
//Type: OnChange
//UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaDate = new GlideAjax('validateDateFieldsUtils');
gaDate.addParam('sysparm_name', 'checkDateCurrentFuture');
gaDate.addParam('sysparm_date', newValue);
gaDate.getXML(validateDateResponse);
function validateDateResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer == 'false') {
g_form.setValue('u_date', '');
g_form.showFieldMsg('u_date', 'he date/time you entered is an invalid format.', 'error');
}
}
}
//Script include:
// Client callable: check
var validateDateFieldsUtils = Class.create();
validateDateFieldsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDateCurrentFuture: function(){
var answer = "";
var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
var selectedDate = selectedDateTime.getDate();
gs.info('SelectedDate: ' + selectedDate);
var currentDate = gs.nowDateTime();
var nowDateTime = new GlideDateTime(currentDate);
nowDateTime.setDisplayValue(currentDate, "dd/MM/yyyy hh:mm:ss");
var nowDate = nowDateTime.getDate();
gs.info('NowDate: ' + nowDate);
if((selectedDate < nowDate) || (selectedDate == null)){
answer = false;
}else{
answer = true;
}
return answer;
},
type: 'validateDateFieldsUtils'
});
The script is working fine. The only problem is that when I select the first day of every month, the return is false. I'll leave some screenshoots below.
Is this a bug or is it something with my script?
Thanks in advance.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 07:56 AM
Hi there,
Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 07:49 AM
Can you show us what's in the log for the value of selectedDate and nowDate from the script include? Also, the script include can be simplified a bit using GlideDate() like so:
var validateDateFieldsUtils = Class.create();
validateDateFieldsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDateCurrentFuture: function () {
var answer = "";
var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
var selectedDate = selectedDateTime.getDate();
gs.info('SelectedDate: ' + selectedDate);
var nowDate = new GlideDate();
gs.info('NowDate: ' + nowDate);
if ((selectedDate < nowDate) || (selectedDate == null)) {
answer = false;
} else {
answer = true;
}
return answer;
},
type: 'validateDateFieldsUtils'
});
Apparently, gs.nowDateTime() uses the system time zone, which may be messing with your expected results: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0594666
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 08:36 AM
Hi Frank,
Here's my log of when I selected day 1. What you said about the time zone makes sense, but I found it odd because it's working fine for the other days.
NowDate: 2022-01-06 (yyyy/MM/dd)
SelectedDate: 2022-01-02 (yyyy/dd/MM).
That's could be the problem. Is there any way to fix this?
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 07:56 AM
Hi there,
Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 09:24 AM
Thanks for your article, Mark! It worked very well to solve my problem.
Thanks again,
Flavio.