how to get age of a records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2025 08:55 AM
I have a task to display record age when form loaded on Configuration Item table.
Example of Expected Output:
When a CI record created on January 1, 2020, is loaded on June 12, 2024, the info message should read: "This record is 4 Years 5 Months 11 Days 0 Hours 0 Minutes old."
How to achieve this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2025 10:16 AM
There is the 'Duration Calculator' script include that you can use to do that. Also see:
SImpleDurationVsRelativeDuration.html
Script example, to get the number of seconds:
// getting the number of seconds based on two date-time values
var testRec = new GlideRecord('cmdb_ci');
testRec.setLimit(10);
testRec.query();
while (testRec.next()) {
var sDate = new GlideDateTime(testRec.sys_created_on);
var eDate = new GlideDateTime(now()); // now
gs.info("Start: " + sDate + ", End: " + eDate);
var sSecs = sDate.getNumericValue() / 1000;
var eSecs = eDate.getNumericValue() / 1000;
var diffSecs = eSecs - sSecs;
gs.info('Name: ' + testRec.name +', sSecs: ' + sSecs + ', eSecs: ' + eSecs + ', diff: ' + diffSecs);
}
(logic in the 'while' loop. Then convert the 'diffSecs' value to Years, months, days, hours, minutes.
You can use an 'On Load' client script that calls the script include that contains the logic to get the total seconds.