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

Prateek kumar
Mega Sage

Hello Rajhan


Can we try something like this:



var dateString1 = '2017-12-26 07:39:52';


var dateString2 = '2017-12-26 09:55:22';


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


diffSeconds = diffSeconds/60;


gs.print(Math.round(diffSeconds));



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

I created an Integer field, Business Downtime and added the below script in the Calculated Value. - Any suggestions?



(function calculatedFieldValue(current) {




// Add your code here


var dateString1 = g_form.getValue('begin');


var dateString2 = g_form.getValue('end');


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


diffSeconds = diffSeconds/60;


gs.print(Math.round(diffSeconds));



return '';   // return the calculated value




// })(current);


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

Got it. Awesome.



Thanks for the much help.



I also got another task. Based on the value on this number and values from multiple fields, i need to calculation and get the numbers populated in a separate field.