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

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

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;

return Math.round(diffMins);   // return the calculated value

 

-Anurag

-Anurag

Community Alums
Not applicable

Hi, is this a calculated value? I ideally need this as a business rule if possible?

The same script would run in a BR as well, just instead of return you set it to the field you want.

The BR should run when both begin and end fields have values.

-Anurag

Community Alums
Not applicable

Brilliant! Thanks for your help I used this in the end.

(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 = diffSeconds;

})(current, previous);