- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2022 08:46 AM
Hello,
I have to validate date field for 1 year validation and that should be only year not time.
For example: field 1: Date From
field 2: Date To
"Date To" should be within 1 year from "Date From" and it should not calculate the time.
Currently "Date From" is 27.05.2022 07.48.15 pm and if I give "Date To" as 27.05.2023 08.48.15 pm its showing me the error as its beyond one year.
Below is the onsubmit client script written to extract the date but not sure how to use them in validation.
var DateTo=g_form.getValue('u_date_to').split(' ')[0];
var DateFrom =new Date(g_form.getValue('u_date_from'));
var Diff = Math.abs(DateFrom - DateTo);
var Days = Math.ceil(Diff / (1000 * 3600 * 24));
if(Days > 365) {
alert('Please enter a date within one year from Date From');
return false;
}
Can somebody help me out here.
Thanks,
Shalani
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2022 03:34 AM
Hi Shalani,
Following script will check if the to date is within 1 year from from date.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var from = g_form.getValue('date_from');
var fromDate = getDateFromFormat(from, g_user_date_time_format);
var nextYear = new Date(fromDate);
nextYear = new Date(nextYear.getFullYear() + 1, nextYear.getMonth(), nextYear.getDate());
var toDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));
toDate = new Date(toDate.getFullYear(), toDate.getMonth(), toDate.getDate());
var diff = toDate - nextYear; // milliseconds
diff = diff / (1000 * 60 * 60 * 24);
if (diff > 0) {
g_form.showFieldMsg('date_to', 'Please enter a date within one year from Date From', 'error');
}
} catch (e) {
alert(e.message);
}
}
Execution
case 1: same date, different time
case 2: to date is more that a year
case 3: less than a year

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2022 09:26 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-2022 ServiceNow Community MVP
2020-2022 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
05-27-2022 09:28 AM
Thanks for your response.
But currently there are some validations which I need to do in client script as per the requirement so that's why following this approach.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2022 03:34 AM
Hi Shalani,
Following script will check if the to date is within 1 year from from date.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var from = g_form.getValue('date_from');
var fromDate = getDateFromFormat(from, g_user_date_time_format);
var nextYear = new Date(fromDate);
nextYear = new Date(nextYear.getFullYear() + 1, nextYear.getMonth(), nextYear.getDate());
var toDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));
toDate = new Date(toDate.getFullYear(), toDate.getMonth(), toDate.getDate());
var diff = toDate - nextYear; // milliseconds
diff = diff / (1000 * 60 * 60 * 24);
if (diff > 0) {
g_form.showFieldMsg('date_to', 'Please enter a date within one year from Date From', 'error');
}
} catch (e) {
alert(e.message);
}
}
Execution
case 1: same date, different time
case 2: to date is more that a year
case 3: less than a year
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2022 08:31 AM
Thank you very much