We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

difference between dates in years

samadam
Mega 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

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
Mega Sage

Thank you so much, that worked