ITSM

mahesh009
Tera Contributor

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

Aniket Chavan
Tera Sage
Tera Sage

Hello @mahesh009 ,

The subtract method in GlideDateTime class returns a GlideDuration object, which doesn't have a direct days property. Instead, you should use the getDayPart() method on the resulting GlideDuration object to get the age in days.

 

Here’s how you can modify your script to correctly calculate the age in days:

function calculateRecordAge(record) {
   var now = new GlideDateTime();
   var created = new GlideDateTime(record.sys_created_on);
   var ageDuration = now.subtract(created);
   
   // Use getDayPart() to get the days part of the GlideDuration object
   return ageDuration.getDayPart();
}

 

This should return the correct age in days. Let me know if you have any further questions.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

SANDEEP28
Mega Sage

@mahesh009 Use below code to get output in days. Also check creation date of incident on which you are trying to test. If creation date not more than one day then it will definitely return zero value.

 

 

var createdDateTime = new GlideDateTime(record.getValue('sys_created_on'));
var currentDateTime = new GlideDateTime(); // returns current date and time
var diff = new GlideDateTime.subtract(createdDateTime, currentDateTime);
var days = diff.getDayPart();
return days;

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Moin Kazi
Kilo Sage
Kilo Sage

Hi @mahesh009 ,

 

Below is the correct way to use subtract method with GlideDateTime API -

 

 

 

var pastDate = new GlideDateTime("2024-09-29 20:30:55");
var currDate = new GlideDateTime();
gs.info("pastDate: " + pastDate); // ------------ pastDate: 2024-09-29 20:30:55
gs.info("currDate: " + currDate); // ------------  currDate: 2024-10-17 06:16:14
var dur = new GlideDuration();
dur = GlideDateTime.subtract(pastDate, currDate); // always pass past date as a first parameter
gs.print(dur.getDisplayValue()); // ------------- 17 Days 9 Hours 45 Minutes

 

 

 

Hope this help you.

 

Regards

Moin

Ravi Gaurav
Giga Sage
Giga Sage

Hi @mahesh009 

Just change "return age.days;" ----> return age.getDayPart();
as now.subtract() will return glide duration.

Link :-https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server/c_GlideDurationScopedAPI

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/