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.

Calculate time difference in minutes

Santhana Rajhan
Mega Sage

I want a field to display the difference between two times in minutes.

find_real_file.png

Eg: Business Duration = 136

1 ACCEPTED SOLUTION

Hello Santhana


Please try below. g_form doesnot work server side



(function calculatedFieldValue(current) {



var dateString1 = new GlideDateTime(current.begin); //make sure variable name is begin


var dateString2 = new GlideDateTime(current.end); // make sure variable name is end


var diffSeconds = gs.dateDiff(dateString1, dateString2, true);


diffMins = diffSeconds/60;



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



})(current);



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

View solution in original post

20 REPLIES 20

If your values are subjected to changes, you can create a On change client script and put the result value in your required feild using g_form.setValue()


Client scripts



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

These don't frequently change. These fields will be used post incident from a reporting perspective.


Ok then you can configure a BR



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

Vipin Murugesan
Kilo Sage

Hi Santhana,



Try below code, replace list line with your table name which you want to achieve.




var gr = new GlideRecord('Table Name');


gr.addQuery('active',true);


gr.query();


while(gr.next())


{


var holder = gs.dateDiff(gr.getDisplayValue('begin'), gr.getDisplayValue('end'),false) ;


holder = holder.split(' ')[1];


gs.print(holder);


}


benn23
ServiceNow Employee
ServiceNow Employee

You can set the 'max_units' attribute in that field's dictionary record.   Add this attribute:   max_unit=minutes