The CreatorCon Call for Content is officially open! Get started here.

How to validate date field for checking 1 year validation in client script

shalani1
Tera Contributor

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

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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

find_real_file.png

case 2: to date is more that a year

find_real_file.png

case 3: less than a year

find_real_file.png

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

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.

Hitoshi Ozawa
Giga Sage
Giga Sage

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

find_real_file.png

case 2: to date is more that a year

find_real_file.png

case 3: less than a year

find_real_file.png

Thank you very much @Hitoshi Ozawa. This script works very well.