Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

difference between dates in years

samadam
Kilo Sage

I am trying to find difference between current date and start dates in years, this is in scoped applications. Datediff is not working. Any other ways to get this?

1 ACCEPTED SOLUTION

sunil maddheshi
Tera Guru

@samadam 

Please try below approach

var startDate = new GlideDateTime("2020-02-19"); // Replace with actual field value
var currentDate = new GlideDateTime();

var diffMillis = currentDate.getNumericValue() - startDate.getNumericValue();
var diffYears = diffMillis / (1000 * 60 * 60 * 24 * 365); 

gs.info("Difference in years: " + diffYears);

Please mark correct/helpful, thanks

View solution in original post

6 REPLIES 6

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @samadam  i have try in background script for incident table to check it work . make changes as per your requirement 

var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
if (!gr.opened_at.nil()) {
var startDate = new GlideDateTime(gr.opened_at);
var currentDate = new GlideDateTime();
 
var startYear = parseInt(startDate.getYearUTC(), 10);
var currentYear = parseInt(currentDate.getYearUTC(), 10);
 
var difference = currentYear - startYear;
 
gs.info('Incident: ' + gr.number + ' | Year Difference: ' + difference);
}
}
Screenshot 2025-02-19 at 10.57.00 AM.png

samadam
Kilo Sage

Thank you so much, that worked