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.

ITSM

mahesh009
Tera Expert

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;

}

11 REPLIES 11

Sandeep Rajput
Tera Patron
Tera Patron

@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;
}

Ramesh_143
Giga Guru

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