We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Adding days to date field in client script

Rudi2
Tera Guru

Hi Guys

I have the following Client script, but I want to add 2 days to the one variable called 'today'. Any ideas?

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

     

      if (isLoading || newValue == '') {

             

              return;

 

      }

      var reqDate = new Date(newValue);

      var today = new Date(); //add 2 days to this var    

      if(reqDate <= today){

             

              alert('Date must not be in the past and at least 2 days from now');          

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

             

      }

     

     

}

Thanks

3 REPLIES 3

Chuck Tomasi
Tera Patron

Hi Rudi,



Doing this on the client side is somewhat unreliable. Here's a collection of useful time/date scripts that can help (one of which is to add date/time.)



Client Script Date/Time Functions


Hi ctomasi



Okay, im getting this to work. The only thing I need from this is to get the value from today's date and not the selected date like in this line:



var cdt = g_form.getValue('some_date'); //Choose the field to add time from



Then I need to compare it and get a result.



The end result will be not to be able to select a date in the past up until 2 days from now. I hope it makes sense..   Am I missing something?


Hi Rudi,



The other functions in that linked script should do the trick for you. Let the user pick a date, then use an onChange client script to call the appropriate date comparison function.