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

OlaN
Giga Sage
Giga Sage

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);

 

Rohit99
Mega Sage

Hi @mahesh009,
You may try with the following script.


var now = new GlideDateTime();
var created = new GlideDateTime(gr.sys_created_on);
var dur = GlideDateTime.subtract(created, now);
var durationString = dur.getDisplayValue();
var days = parseInt(durationString.split(" ")[0]);

Rohit99_0-1729148803171.png

 

 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

Sai Krishna6147
Mega Guru

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 !!

 

brahmandlapally
Mega Guru

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
}

thullurishalini
Kilo Guru

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 

thullurishalini_0-1729266542953.png

 

Correct if this solves your query and also mark 

thullurishalini_1-1729266542961.png

 

Helpful if you find my response worthy based on the impact.


thanks,

Shalini.