
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2016 11:39 AM
I have 3 fields on a form. Two of those fields are date fields and the other is a Single line text field.
What I am looking for is take the two date fields for example if the Incurred Period From is December 2016 and the Incurred Period To is May. I want the # of adjustments field to have the calculated value of months from December to May.
Thanks,
Karen
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2016 12:38 PM
Use this and let me know how it goes
var total_months=0;
var months=12;
var gDate = new GlideDate();
gDate.setValue(current.<field name>);
var gDT = new GlideDateTime(gDate);
var months=gDT.getMonthLocalTime();
var year=gDT.getYearLocalTime();
var gDate1 = new GlideDate();
gDate1.setValue(current.<field name1>);
var gDT1 = new GlideDateTime(gDate1);
var months1=gDT1.getMonthLocalTime();
var year1=gDT1.getYearLocalTime();
if(year != year1){
total_months=(12-months)+(months1-1)+(year1-year-1)*12;
}
else{
total_months=months1-months-1;
}
current.<field name2>=total_months;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2022 11:13 AM - edited ‎10-10-2022 11:15 AM
Hello this will help you.
no need to do condition
total_month= (secondDateMonth - firstDateMonth )+ (secondDateYear - firstDateYear) * 12;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-09-2017 06:08 AM
Good morning Abhinay,
This was the correct answer. The script had to be put in a script include and then referenced from the catalog client script. When I included the script just as a catalog client script it wasn't working. I do have one more question if that is okay. Below is the script that is in the script include and the catalog client script.
When I was testing I noticed that instead of just the # of months = the number. It is adding on a ".0" to the number. Why is that?
Also I tested by putting in an Incurred Period From Date a few years back and the calcuation is adding on additional months. Can you assist with this ?
Script Include:
var MonthCalc = Class.create();
MonthCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
monthDiff: function(){
var total_months = 0;
var gDate = new GlideDate();
gDate.setValue(this.getParameter('sysparm_from'));
var gDT = new GlideDateTime(gDate);
var months=gDT.getMonthLocalTime();
var year=gDT.getYearLocalTime();
var gDate1 = new GlideDate();
gDate1.setValue(this.getParameter('sysparm_to'));
var gDT1 = new GlideDateTime(gDate1);
var months1=gDT1.getMonthLocalTime();
var year1=gDT1.getYearLocalTime();
if(year != year1)
total_months=(12-months)+(months1+1)+(year1-year-1)*12;
else
total_months=months1-months+1;
return parseInt(total_months);
},
type: 'MonthCalc'
});
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//var today = new Date();
var ga = new GlideAjax('MonthCalc');
ga.addParam('sysparm_name','monthDiff');
ga.addParam('sysparm_from',g_form.getValue('ad_incur_per_from'));
ga.addParam('sysparm_to',newValue);
ga.getXML(callBack);
function callBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert('answer ' + answer);
g_form.setValue('ad_adj_num', answer);
/*
var dateMS = today.getTime();
dateMS = dateMS + (10*24*60*60*1000);
var newDT = new Date();
newDT.setTime(dateMS);
answer = newDT;
alert(answer);*/
// if (newValue < answer.substring(0,10)) {
// alert('Date of application rollout needs to be at least 10 days from current date');
// g_form.setValue('package_rollout_date', '');
// }
//g_form.setValue('sales_needed_by',answer.substring(0,10));
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 12:18 AM
Hello,
Thank you for the solution! I had a similar requirement and it works perfectly.
Have a great day,
Elena

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 07:45 AM
Elena,
Would you mind sharing your solution? I tried the code listed above and it is not calculating correctly as mentioned by Cupcake.
Thanks,
Marty

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 10:36 PM
Hi,
Sure! Here it is:
var totalMonths = 0;
var gDate = new GlideDate();
gDate.setValue(field1);
var gDT = new GlideDateTime(gDate);
var months = gDT.getMonthUTC();
var year = gDT.getYearUTC();
var gDate1 = new GlideDate();
gDate1.setValue(field2);
var gDT1 = new GlideDateTime(gDate1);
var months1 = gDT1.getMonthUTC();
var year1 = gDT1.getYearUTC();
if(year != year1){
totalMonths = (12-months)+(months1+1)+(year1-year-1)*12;
}
else{
totalMonths = months1-months+1;
}
var monthsBetweenFields = totalMonths;
Have a nice one!