Using JavaScript to manipulate a date

The below JavaScript functions can be used to manipulate a date within CallScripter. To re-use this code, no changes would be required to the AddZeroToDatepart() and AddDaysToDate() functions instead you need to configure how the AddDaysToDate() function is called. This needs to be in the below format:
NewDate = AddDaysToDate(OriginalDate,"DateFormat",NumberOfDaysToAdd);
An example of this with CallScripter fields included would be
[Call Back Date] = AddDaysToDate([Appointment Date],"mm/dd/yyyy",-3);
This would take the input field of [Appointment Date] in the format of 12/25/2016 (mm/dd/yyyy) for example and remove 3 days from this date, returning a value of 12/22/2016 into the [Call Back Date] field.
Things to consider when using this code
  • It is recommended this JavaScript code is used in conjunction with the Date Field in CallScripter as the input Date, as this gives the user a date picker to select the date from.
  • The date control can be configured with many date formats, therefore need to ensure that the same dateformat is being passed into the AddDaysToDate() function.
  • Positive or negative numbers can be passed into this function to either add or subtract days from the input Date.
  • Another consideration is what to do with any errors that are thrown by the function. In the code snippet below the value of the error is written to a script variable, this error is then displayed within a text label control and this control is made visible to the agent. It would also be possible to alert any errors to the user by using alert(ex); in the catch section rather than [var_error] = ex;
function AddZeroToDatepart(datepart_raw) {
	try {
		var datepart = parseInt(datepart_raw);
		if (isNaN(datepart)) {
			throw 'Passed in date is not a number';
		if (datepart < 10) {
		    datepart = '0' + datepart;
		return datepart;
	catch (ex) {
		throw 'Error adding zero to datepart: ' + ex;

function AddDaysToDate(inputDate,dateFormat,numberOfDays){
	try {
        dateFormat = dateFormat.toLowerCase();
		var dayPosition = dateFormat.indexOf("d");
		var monthPosition = dateFormat.indexOf("m");
		var yearPosition = dateFormat.indexOf("y");
		var day = inputDate.substr(dayPosition,2);
		var month = inputDate.substr(monthPosition,2) -1;
		var year;

		if ("yyyy") >= 0){
		    year = inputDate.substr(yearPosition,4);
		else {
		    year = inputDate.substr(yearPosition,2)
		var date = new Date(year,month,day);
		var newdate = new Date(date);
		newdate.setDate(newdate.getDate() + numberOfDays)
		var newday = newdate.getDate();
		var newmonth = newdate.getMonth() + 1;
		var newyear = newdate.getFullYear().toString();
		if (year.length === 2)
			newyear = newyear.substr(2,2);

		newmonth = AddZeroToDatepart(newmonth);
		newday = AddZeroToDatepart(newday);

        return dateFormat.replace("dd",newday).replace("mm",newmonth).replace("yyyy",newyear).replace("yy",newyear);
	catch (ex) {
		throw 'Error adding days to Date: ' + ex;


if ([Date Field] != '') {
    try {
        [Text Box] = AddDaysToDate([Date Field],"dd/mm/yyyy",-2);
    catch (ex) {
        [var_error] = ex;