Date field on catalog item can not be prior to the current day's date

Cupcake
Mega Guru

I have a date field where I want to ensure that the date selected from the date variable is not greater than the current days date. I have the following code for another date field that has to be 5 days out. Not very good with the date thing yet, so 1) should i have something like a (-1) in my dateMS portion of the script. And 2) will it render an error if the customer tries to select a date in the past or should I include some code that has an alert type of box stating something like "Please select a future date"......

var today = new Date();

var dateMS = today.getTime();

dateMS = dateMS + (5*24*60*60*1000);

var newDT = new Date();

newDT.setTime(dateMS);

g_form.setValue('ftr_needed_by',formatDate(newDT,g_user_date_format));

Thank you,

Karen

1 ACCEPTED SOLUTION

zica
Giga Guru

Karen,



Nothing wrong with the code, what happening is that 'getUTCDate() returns the day;


The reason that it did not allow you to select 2nd March is because 2 is lesser than 8 (current day) and conversely 16 (febr) is greater than 8 !!


I will suggest you to try with dateDiff() method to calculate the difference between two date/times given.



let me know if you have more concerns



Kind regards


Do not feel shy to mark correct or helpful answer if it helps or is correct



View solution in original post

18 REPLIES 18

Thank you I created an onChange catalog client script and I am receiving the following error. I tried adding a ), but didn't get any better.



find_real_file.png


Karen,



there was a pb with the row 17 and 19.


Here is the corrected script :


function verifyDates() {


  var dReqStart = new GlideDateTime();


  dReqStart.setDisplayValue(current.u_req_start_date);


  var dReqEnd = new GlideDateTime();


  dReqEnd.setDisplayValue(current.u_req_end_date);


  var today = new GlideDateTime();


  today.setDisplayValue(gs.now());


  if ((dReqStart.getNumericValue() - today.getNumericValue())/(1000*60*60*24) < 0) {


  gs.addErrorMessage("Request Start Date cannot begin before today.");


  current.setAbortAction(true);


  } else if ((dReqEnd.getNumericValue() - dReqStart.getNumericValue())/(1000*60*60*24) < 0) {


  gs.addErrorMessage("Request End Date cannot come before Request Start Date.");


  current.setAbortAction(true);


  }



}



Kind regards,


ZA


Do not feel shy to mark correct or helpful answer if it helps or is correct


Dear AKb,


        Sorry to be a bother but this is still not working. I changed the script and I receive the following error. Because of the error about the gs not being used in client scripts, I even tried changing that to alert and still no good.



find_real_file.png


Hi Karen, could you please try this structure:



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


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


        return;


  }



// your logic goes here...


}



And please use g_form.getValue and g_form.setValue functions accordingly.


This is a wiki you can refer too Useful Catalog Scripts - ServiceNow Wiki



Hope this will help you.



BR,


/Suraj


This is a script that i have used, may be modify accordingly to your purpose.


Hope this will help you.




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


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


          return;


    }



    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 delivery_date


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


                var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);                    


               


   


                if (startDateNum <= 0){  


                      alert('Please use the calendar icon to select a date.');  


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


                } else if (startDateNum < currentDateNum) {  


                      alert('You cannot select a date in the past.');  


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


                }


          }  


    }  


}




BR,


/Suraj