ITSM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2024 10:49 PM
I'm trying to create a custom script include to calculate the age of a record in days. However, the result is always 0. What am I doing wrong?
script:
function calculateRecordAge(record)
{
var now = new GlideDateTime();
var created = new GlideDateTime(record.sys_created_on);
var age = now.subtract(created);
return age.days;
}
- 849 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 06:59 PM
@mahesh009 Apart from what others suggested, you can try the following as well.
function calculateRecordAge(record)
{
var created = new GlideDateTime(record.sys_created_on);
var now = new GlideDateTime();
var diff = GlideDateTime.subtract(now, created);
var age = diff.getNumericValue() / (1000 * 60 * 60 * 24);
return age;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 10:15 PM
Hi @mahesh009 ,
Try the below code once which is corrected version from my side and let me know if you face any difficulties
function calculateRecordAge(record) {
var now = new GlideDateTime();
var created = new GlideDateTime(record.sys_created_on);
var age = now.subtract(created);
return age.getDays(); // Use getDays() to get the difference in days
}
Thanks,
Ramesh