The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Verify that duration date is no more than an year from now based on the date of another variable.

ramosp
Kilo Explorer

The user is prompted to enter a date that is no more than a year from now. I need to check that the date is valid when it is entered. Is anyone working on something similar or has a script or business rule doing this already. This is on a service catalog item.      

10 REPLIES 10

manikorada
ServiceNow Employee
ServiceNow Employee

Pablo,



Are you prompting an Input screen where they will set the date


This a date variable on the form where they select the date


manikorada
ServiceNow Employee
ServiceNow Employee

Pablo,



You can have a Client Script like:



var date = getDateFromFormat(g_form.getValue('<<date>>'),g_user_date_format);


var today = new Date();


    if((date.getTime() - today.getTime()) > (364 * 24 * 60 * 60 * 1000))


          {


          alert('Date should be below one year);


          }


dartx21
Kilo Contributor

Hi I have tried the following script and I am having no success for my Date field. I am trying to limit the data to one year or less on my Record Producer Field.



function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
          return;
    }


    //Type appropriate comment here, and begin script below
var date = getDateFromFormat(g_form.getValue('u_resolution_date'),g_user_date_format);


var today = new Date();


    if((date.getTime() - today.getTime()) > (364 * 24 * 60 * 60 * 1000))


          {


          alert('Date should be below one year');
    }
}


germc
Giga Expert

Hi guys,


I got the functionality to work through to following Catalogue Client Script: (NB, 'contract_end_date' is the name of my field)



function onChange(control, oldValue, newValue, isLoading) {  


    if (!isLoading) {  


          if(newValue != '') {  


                //current date  


                var currentDateObj = new Date();  


                var currentDateStr = formatDate(currentDateObj, g_user_date_format);  


                var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);  


 


//get start date  


var startDateStr = g_form.getValue('contract_end_date');  


var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);


                  var today = new Date();


                               


                if (startDateNum - today.getTime() > (365 * 24 * 60 * 60 * 1000)) {  


                      alert('Date must be less than one year');  


                      g_form.setValue('contract_end_date', '');


                        }else{


                      if (!startDateNum) {  


                      alert('Invalid Date. Please select the date picker and try again.');  


                      g_form.setValue('contract_end_date', '');


                      }


               


                }


          }  


    }  


}  




NB: Also, by adding in the following piece of code at the end prevents users entering jargon into the field. (IE, they have to enter a valid date format):



}else{


                      if (!startDateNum) {  


                      alert('Invalid Date. Please select the date picker and try again.');  


                      g_form.setValue('contract_end_date', '');


                      }