Add 6 months to a variable date (on change) and display result in another field.

rao_2017
Kilo Contributor

Hi ,

  We have a two variables in a catalog item start and end date if the start date is changed then the changed date should add 6 months and display the output in End date variable.

 

 Does anyone have done idea on this?

 

Done with the catalog client script using the below script this returns an error while the date format is dd-mm-yyy and dd/mm/yyyy

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateTimeAjaxFunctions');// DateTimeAjaxFunctions is a script include 
ga.addParam('sysparm_name','nowDateTime');
ga.getXML(callback);
var retdt = g_form.getValue('end_date');
function callback(response){
var now = response.responseXML.documentElement.getAttribute("answer");
ga.addParam('sysparm_name','addMonths');
ga.addParam('startDate',newValue);
ga.addParam('monthsToAdd',6);
ga.getXML(callback1);
}
function callback1(response)
{
var sixMonthsFromNow = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('end_date',sixMonthsFromNow);
}
}

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rao,

Can you share the script include code?

Also are you using scoped application here?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rao,

Here is a sample script

var date = new GlideDate();

var value = this.getParameter('startDate');

var toAdd = this.getParameter('monthsToAdd');
date.setValue(value);
datee.addMonths(toAdd);
gs.print(datee.getDate());

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi ,

 

var DateTimeAjaxFunctions = Class.create();
DateTimeAjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {

nowDateTime: function () {
// Current date/time
return gs.nowDateTime();
},

nowLocalTime: function() {
return toLocalTime(nowDateTime);
},

toLocalTime: function(nowDate) {
var secondsToAdd = (nowDate.getTZOffset() / 1000); // Get the timezone offset in milliseconds
nowDate.addSeconds(secondsToAdd); // Convert to local time
return nowDate;

},

addDays: function() {

var startDate = new GlideDateTime(this.getParameter('startDate'));
var daysToAdd = this.getParameter('daysToAdd');
startDate.addDaysLocalTime(daysToAdd); // Convert to local time
return startDate;
},

addMonths: function() {

var startDate = new GlideDateTime(this.getParameter('startDate'));
var monthsToAdd = this.getParameter('monthsToAdd');
startDate.addMonthsLocalTime(monthsToAdd); // Convert to local time
return startDate;
},

dateDiff: function(){

var date1 = new GlideDateTime(this.getParameter('date1'));
var date2 = new GlideDateTime(this.getParameter('date2'));

var diffSeconds = gs.dateDiff(date1, date2, true);
return diffSeconds;
}
});

 

Hi Rao,

So what is the script include returning you?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader