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;
}
- 847 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 12:02 AM
Hi,
Like Aniket already showed, you must use built in functions from GlideDuration to retrieve the day part of the object returned.
I would like to add an additional detail.
Always aim to make your code more robust.
In the given example there is no error checking if the object is valid and exists. Always perform these checks to avoid unexpected errors.
Example below:
TimeStuff.prototype = {
initialize: function() {
},
getDayPart : function(record){
if (!record || !record.isValid()){
return 0;
}
else{
var today = new GlideDateTime();
var created = record.getValue('sys_created_on');
var createdDate = new GlideDateTime(created);
var diff = GlideDateTime.subtract(createdDate, today);
return diff.getDayPart();
}
},
type: 'TimeStuff'
};
This script include would then be called from a business rule or similar using this syntax:
var dayDiff = new global.TimeStuff().getDayPart(someGlideRecordObject);
gs.info('diff: ' + dayDiff);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 12:07 AM
Hi @mahesh009,
You may try with the following script.
Please mark my response as correct and helpful if it helped solved your question.
Thanks,
Rohit Suryawanshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 05:39 AM
Hi @mahesh009
The issue lies in the subtract() method. While it calculates the difference between the two dates, it returns a GlideDuration object, which represents a duration of time. To get the difference in days, you need to access the days property of the GlideDuration object.
Corrected Script Include:
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}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 07:51 AM
Hi @mahesh009
Use the below code
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 08:49 AM
Hi @mahesh009
The issue with your script is that the subtract method in the 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:
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();
}
Please Mark
Correct if this solves your query and also mark
Helpful if you find my response worthy based on the impact.
thanks,
Shalini.