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.

Difference between start created date time and updated date time

Mark Wood
Tera Contributor

hello Experts,

I have written a script  to calculate difference between created date of incident  and  updated  date of incident.

I have attached output screenshot below please and guide me.

Thank You.

2 REPLIES 2

Shruti
Giga Sage
Giga Sage
var grInc = new GlideRecord('incident');
grInc.addQuery('number', ' ' );  //query with Incident no.
grInc.query();
if(grInc.next()){
var created_date_time = new GlideDateTime(grInc.sys_created_on);
var updated_date_time = new GlideDateTime(grInc.sys_updated_on);
var dur_seconds = gs.dateDiff(updated_date_time, created_date_time, true);
var dur_hours = Math.round(dur_seconds / 3600);
}

Amit Gujarathi
Giga Sage
Giga Sage

HI @Mark Wood ,
I trust you are doing great.
Please try below code :

// Assuming you have the created and updated dates stored in variables
var createdDate = new GlideDateTime('yyyy-MM-dd HH:mm:ss'); // Replace with the actual created date
var updatedDate = new GlideDateTime('yyyy-MM-dd HH:mm:ss'); // Replace with the actual updated date

// Calculate the difference in milliseconds
var differenceInMs = updatedDate.getNumericValue() - createdDate.getNumericValue();

// Convert the difference to hours
var differenceInHours = differenceInMs / (1000 * 60 * 60);

// Display the result
gs.info('The difference in hours is: ' + differenceInHours);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi