How can we calculate Duration in years and months
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 03:20 AM
Hi,
Contract duration should be equal to “Expiry date – Effective date(start date)”.
Duration should be in years such as 2 years and 6 months
Thanks
Raj

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 03:24 AM
Hi,
please refer to the following thread, it will help you to do it easily:
I would also suggest you to check the following ServiceNow thread as well, there are some other interesting good idea:
If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
Thank you
Cheers
Alberto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 04:51 AM
I tried in the below one,but it's not working
var gr=new GlideRecord('u_contract');
gr.query();
while(gr.next()){
duration = gs.dateDiff(gr.current.u_expiry_date.getGlideObject().getDisplayValue(),gr.current.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("Duration:" + durationYears + 'Years'+durationMonths+ 'Months');
}
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 04:53 AM
Try to put some log and share what is the outcome please.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 05:14 AM
I'm getting in the below way:
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);
gs.log("Duration:"+durationYears + " "+'Years'+ " "+durationMonths+ " "+'Months');
}
O/P:-