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.

Convert the duration of two dates to minutes?

Community Alums
Not applicable

Does anyone have a business rule I can use as an example to convert the duration between 2 dates to minutes?

We currently have 2 date fields (dd/mm/yyyy hh:mm:ss)
u_outage_start
u_outage_end

Looking to get this converted to minutes in field u_reported_downtime_degradation

Thanks in advance!

1 ACCEPTED SOLUTION

Yes,

That is because you are setting the seconds variable and not mins, Use the below

 

(function executeRule(current, previous /*null when async*/) {
var dateString1 = new GlideDateTime(current.u_outage_start); 
var dateString2 = new GlideDateTime(current.u_outage_end); 
var diffSeconds = gs.dateDiff(dateString1, dateString2, true);
diffMins = diffSeconds/60;
	current.u_reported_downtime_degradation = diffMins ;

})(current, previous);
-Anurag

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

Hi Arung,

After some testing its working out its actually calculating the seconds not minutes?

Any amendments I can make?

Yes,

That is because you are setting the seconds variable and not mins, Use the below

 

(function executeRule(current, previous /*null when async*/) {
var dateString1 = new GlideDateTime(current.u_outage_start); 
var dateString2 = new GlideDateTime(current.u_outage_end); 
var diffSeconds = gs.dateDiff(dateString1, dateString2, true);
diffMins = diffSeconds/60;
	current.u_reported_downtime_degradation = diffMins ;

})(current, previous);
-Anurag

Community Alums
Not applicable

Of course! Can you tell its Friday? 🙂

haha. Cheers mate!!

-Anurag