We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Difference between start created date time and updated date time

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

HI @abhijeetpaw ,
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