Calculate the # of months between two date fields

Cupcake
Mega Guru

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.

find_real_file.png

Thanks,

Karen

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

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;


View solution in original post

14 REPLIES 14

drjohnchun
Tera Guru

This post might help How to calculate months difference between given Start Date and End Date in the form?



Are you trying to do this on the client or server side?


Also, according to your definition, is the # of months between Nov 27, 2016 and Dec 1, 2016, for example,


  • 0 (because Incurred Period To < Dec 27)
  • 1 (it's 12 - 11), or
  • 2 (count both Nov and Dec)


Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


Thank you John in our case it would be 2 to count both November and December.



Karen


Abhinay Erra
Giga Sage

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;


Good morning Abhinay,


        I am finally get back to this. I have this script in place and it doesn't seem to be calculating the # of adjustments field. Here is my script below:



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


     


var months=12;



var gDate = new GlideDate();



gDate.setValue('ad_incur_per_from');



var gDT = new GlideDateTime(gDate);



var months=gDT.getMonthLocalTime();



var year=gDT.getYearLocalTime();



var gDate1 = new GlideDate();



gDate1.setValue('ad_incur_per_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;



}



total_months=months1-months-1;



}



ad_adj_num= total_months;


find_real_file.png



find_real_file.png