How can we calculate Duration in years and months

Raj90
Tera Guru

Hi,

Contract duration should be equal toExpiry date – Effective date(start date)”.

Duration should be in years such as 2 years and 6 months

find_real_file.png

 

Thanks 

Raj

9 REPLIES 9

For example:

Effective date(start date) = 2019-04-15  and 

Expiry date (end date)= 2021-04-15


  output:   Duration: 0 Years 0 Months0 Years 0 Months-474 Years -6 Months-

Code:

var gr=new GlideRecord('u_contract');
gr.query();
while(gr.next()){
var duration = gs.dateDiff(gr.u_expiry_date.getGlideObject().getDisplayValue(),gr.u_effective_date.getGlideObject().getDisplayValue(), true);

var durationYears = Math.floor(duration/365);
var durationMonths = Math.floor((duration % 365)/30);
var durationDays = Math.floor((duration % 365)%30);


template.print(durationYears + " "+'Years'+ " "+durationMonths+ " "+'Months');

}

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi Raj,

 

If Effective date and Expiry date are Date fields, you can use the dateDiff() method, however in your example you've got the arguments backwards - you should pass the start date first, and the end date second:

dateDiff(String startDate, String endDate, Boolean numericValue)

 

From there you can convert the dateDiff string output to a GlideDuration object, and display the duration in Days, Minutes, Seconds:

var duration = new GlideDuration(gs.dateDiff(gr.getDisplayValue('u_effective_date'), gr.getDisplayValue('u_expiry_date'))).getDisplayValue();

Output: 731 Days

 

Or you can convert the dateDiff string output to a GlideDuration object, use the getDayPart() method to get the number of days, and then do some quick math to return the approximate number of years and months (for the sake of simplicity, this assumes that each year is 365 days, and each month is 30 days):

var duration = new GlideDuration(gs.dateDiff(gr.getDisplayValue('u_effective_date'), gr.getDisplayValue('u_expiry_date')));
var days = duration.getDayPart();
var years = Math.round(days / 365);
var months = Math.round(days / 30) - (years * 12);
duration = years + ' Years';
if (months > 0)
  duration += ', ' + months + ' Months';

Output: 2 Years

 

If Effective date and Expiry date are Date/Time fields, you should use the GlideDateTime subtract() method instead.

 

Cheers!

Thanks Sheldon!

When i used the above scripts, my system says I need to use 'GlideDateTime.subtract' instead of 'dateDiff'. I tried that, still the output field shows nothing. This is the script i used:


var todaY = new GlideDateTime();
var thiSYear = todaY.getYearLocalTime();
var doB = new GlideDateTime(current.date_of_birth.toString());
doBYear = doB.getYearLocalTime();
var duration = new GlideDuration(gs.GlideDateTime.subtract(gr.getDisplayValue('doBYear'), gr.getDisplayValue('thiSYear')));
var days = duration.getDayPart();
var years = Math.round(days / 365);
var months = Math.round(days / 30) - (years * 12);
duration = years + ' Years';
if (months > 0)
duration += ', ' + months + ' Months';
if (days > 0)
duration += ', ' + days + ' Days';
current.u_age = duration;

 

I am a newbie.

Hi Narayana,

Indeed, for Date/Time fields you should use the GlideDateTime subtract() method instead of the GlideSystem dateDiff() method - perhaps I shouldn't have hidden that bit at the bottom of my original reply, but you'll find similar guidance in the GlideSystem reference docs.

Try getting the duration like this:

var nowGdt = new GlideDateTime();
var dobGdt = new GlideDateTime(current.date_of_birth);
var dur = new GlideDuration(GlideDateTime.subtract(dobGdt, nowGdt));

In this case, it will be important to understand that if u_age is a Duration field, the display value will be in Days/Minutes/Seconds, not Years/Months/Days. The most appropriate field type will of course depend mostly on how you plan to use the value.

Deon Oosthuizen
Tera Contributor

Hi Raj,

I had the exact same requirement and the below worked like a dream for me.

I created a string field called Duration  and utilized the Calculated Value combined with the below script to achieve what you have explained.

 

 

(function calculatedFieldValue(current) {
    var starts = new GlideDateTime(current.starts).toString();
	var ends = new GlideDateTime(current.ends).toString();

	var duration = new GlideDuration(gs.dateDiff(starts, ends));
	var days = duration.getDayPart();
	var years = Math.floor(days / 365);
	var months = Math.floor((days/30 - years * 12));

	if(years > 0){
		return years + ' years and ' + months + ' months'; 
	} else {
		return months + ' months'; 
	}

    // return the calculated value

})(current);

 

 

 

Hope this helps