Skip to main content
Kinetic Community

Date Add and Compare

Import Files
Service Item DateAddandCompare.zip
KURL Date Add and Compare.rb
How to add a number of Days to a Date Question and then to enforce that the end user doesn't Change your Default Date setting.

Description

A common question that Support receives is how to add a number of Days to a Date field and to ensure that the number of Days is enforced.  The use case for this type of functionality is a Request can not have a "Requested Completion Date" of less than 7 Days from "Today".

To accomplish the Date Add and Compare you will need to take the following Steps:

1) Add an Event to your Page that runs on a Load and has a Custom Action.  This Event will call the dateAdd7(); function.

2) Add an Event to your Page that runs on beforeSubmit and has a Custom Action.  This event will call the dateCheck(); function.

3) Add the following to your Custom Header Content (this defines your custom function called from steps 1 and 2 above):

<script>

function dateAdd7(KS00D0B7A9C48546bpSwXE37YwocLO){
// The questionID variable is the question that will have the days added to it.
//There are no quotation marks around the questionID variable listed above.
// It is on the advanced tab of the question

// nowDate holds the current date
var nowDate = new Date();


// Add 7 days to nowDate
nowDate.setDate(nowDate.getDate()+ 7) ;

// The array builds up the values needed by the setDateFileds function if it is called
var nowDateArray = new Array();
nowDateArray[0] = nowDate.getFullYear();
nowDateArray[1] = nowDate.getMonth() + 1;
nowDateArray[2] = nowDate.getDate();

// Sets the QuestionID with the new value.
KD.utils.Action.setDateFields('KS00D0B7A9C48546bpSwXE37YwocLO', nowDateArray);

//The questionID above must have single quotes.

}

// This function is called in the beforeSubmit event on the page the holds the related 
//date question

function dateCheck(questionID){
// The questionID variable is  for the Requested date question to check
// It is on the advanced tab of the question

// nowDate holds the current date
var nowDate = new Date();

// The array builds up the values needed by the setDateFileds function if it is called
var nowDateArray = new Array();
nowDateArray[0] = nowDate.getFullYear();
nowDateArray[1] = nowDate.getMonth() + 1;
nowDateArray[2] = nowDate.getDate();

// document.getElementByID gets the value of the hidden date question from the template

var surveyDate=document.getElementById("SRVQSTN_" + questionID).value;

// The Hidden date field stores the date as a character string.
// The following steps get the individual values, turns them into integers,
// and makes them into a readable date format
     surveyDateString = surveyDate.split('-');

     var surveyDateInt = new Array();

     surveyDateInt[0] = parseInt(surveyDateString[0],10);
     surveyDateInt[1] = parseInt(surveyDateString[1],10);
     surveyDateInt[2] = parseInt(surveyDateString[2],10);

     var surveyDateFinal = new Date();

     surveyDateFinal.setFullYear(surveyDateInt[0], (surveyDateInt[1] - 1), surveyDateInt[2]);


//Add 7 days from nowDate
     nowDate.setDate(nowDate.getDate()+7) ;

// Finally, we check to see if the Request Date value is greater than 7 days from now
     if (nowDate>surveyDateFinal)
     {
          // Personalized message to the user that date is not acceptable
          alert('Requested Date must be at least 7 days from the Current Date');
          KD.utils.Action.setDateFields(questionID, nowDateArray);
          return false;  //A return value of false will stop the submit process
     }


}

</script>

As seeing this type of functionality in action always helps I have attached a Service Item called JMA Date Add to this posting.  Please note that when you import this Service Item into your environment it will be added to a Service Catalog called JMA Testing.  As you probably don't have this Service Catalog you will need to move the JMA Date Add Service Item into one of your Catalogs.

The JMA Date Add Service Item only contains two questions (Current Date and Future Date).  The Current Date question will display the Current Date and is really used for a sanity check.  When I was building this Service Item I wanted to ensure that I was adding the correct number of days.  The easiest way to accomplish this was to add another Date field to the Service Item.  The Future Date question will be set to 7 days from nowDate.  Note that nowDate is equivalent to $DATE$ in Remedy.