Date Type Variable Format - 3 Months Validation

dharanikumar
Giga Guru

Hi All,

We have two variables on the catalog form Start Date & End Date

Difference between the variables can not be greater than 3 months.

For Example: If Start Date is 17-Jan-2024 then the End Date (I.e.., 16-Apr-2024) should be less than 3 months
If the user enters after 16-Apr-2024 it should throw an error.

ISSUE: Date type variable returns the below value 

dharanikumar_0-1705435750207.png

 

Logs: 

dharanikumar_2-1705435891088.png

 

How can we achieve this using this format.


Thank you!!

 

1 ACCEPTED SOLUTION

So have you looked into no code date validation at all? The article also contains an example of what you are after, only thing is the example is on hours, which you can just choose a different operator for.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

16 REPLIES 16

Joe Wong
Tera Guru

Hi There,

 

You might need a script on your second date dropdown to calculate if the date is valid.  Here is a sample script I developed.

**********************************************************************************

 

var date1 = new GlideDateTime();
// set date1 to your first variable on the form
date1.setValue("04-10-2024");
 
// calculate 3 months from now.
var dateLimit = new GlideDateTime();
// this date is same as date 1
dateLimit.setValue("04-10-2024");
// this will add 3 months to dateLimit
dateLimit.addMonthsUTC(parseInt(3));

var date2 = new GlideDateTime();
// set date2 to your second variable on the form
date2.setValue("07-09-2024");

if(dateLimit > date2) {
    gs.print("less than 3 months");
}
 
**********************************************************************************
 
When the user selects the second variable, run a script to will get the first date, then advance the first date to 3 months ahead by using the "addMonthsUTC(parseInt(3))" function.  Then compare the second date with the first one to determine if it is less than 3 months and handle the outcome accordingly.
 
Hope this help.
 
Joe

Hi Joe,

Thanks for the reply!!

1. Created a On Change Catalog Client Script.

2. From the Catalog client script i am sending both start date and end date values

3. Now issue is its sending the data value in the below format.

dharanikumar_0-1705440321654.png
Like alphabets Jan, Feb etc... 

Type of the variable i am using:

dharanikumar_1-1705440611172.png

 

Joe Wong
Tera Guru

On the client script for my Catalog Item, when I am using

 

g_form.getValue("required_by");
// required_by is my variable name
// I output my variable using
alert(g_form.getValue("required_by"));
 
I get the following with my variable is type Date:
JoeWong_0-1705443944007.png

And when my variable is Date/Time, it get this:

JoeWong_1-1705443994560.png

 

Could there be a system setting that is displaying your date is another format?

Hi Joe,

 

What is the Variable type of required_by?

Mine is Date not Date Time

 

Thank you !!