How to calculate Time difference in a form for two times entered

Raju Singh1
Tera Expert

Hi Experts,

I have two customized Date/Time type fields

1st field is   downtim_occured         //Exact Time during which there was a down time

2nd field is Time_fixed                           // The down time over at this time

Now, I need to calculate the total down time which is the difference of Time_fixed and downtime_occured which means

down_time = Time_fixed - downtime_occured

I need to populate this down_time value in my form.for the Facilities Module.

How shall I get this guyz. Please help!!

4 REPLIES 4

Michael Ritchie
ServiceNow Employee

Do you need this done client side or on save of the record?   Here is an example from an onChange client script that takes the work_start and work_end and calculates the difference:


      var starts = g_form.getValue('work_start');


      var startMillis = getDateFromFormat(starts, g_user_date_time_format);


   


      var ends = newValue;


      var endMillis = getDateFromFormat(ends, g_user_date_time_format);


   


      var minutes = (endMillis - startMillis) / (1000 * 60);


      if (minutes.toString != g_form.getValue('u_work_duration')) {


              g_form.setValue('u_work_duration', minutes);


      }



The u_work_duration is an Integer type field.


darius_koohmare
ServiceNow Employee

If you don't need it to run client side you can make a business rule on that table and add in Something along these lines:



// Given two date/times as strings


var dateString1 = current.u_downtime_occured;


var dateString2 = current.u_time_fixed;



// Determine the difference as number of seconds (returns a string)


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



current.down_time = diffSeconds;



More info client side:


Find difference between two dates



Also check the examples here: Set a duration field value


Raju Singh1
Tera Expert

Hi Guys,



What should be the type of my time difference field down_time. Should it be Integer field. If its integer then what shall be the output, will it be in Minutes,


I need output as either in Minutes, hours or days.. So, can I take the type down_time it as Date/Time filed or It shall be Integer..


Basically my question is Integer will give value in which format..Will it be days or Minutes/Hours/seconds


Raju Singh1
Tera Expert

Hi Michael,


I tried using your script but it didnt work.



However, I have used the script as provided below and also providing the screen shots. could you please check where am I wrong. I think I need to take the field for Down time (u_integer_4) as Date/Type



Here are the screens shots and the client script and the server side script.


Client script


function onChange(control, oldValue, newValue, isLoading)


{var strt = g_form.getValue('u_glide_date_time_1');


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


var ajax =new GlideAjax('AjaxDurCalc');


  ajax.addParam('sysparm_name','durCalc');


  ajax.addParam('sysparm_strt',strt);


  ajax.addParam('sysparm_end',end);


  ajax.getXMLWait();var answer = ajax.getAnswer();


  g_form.setValue('u_integer_4', answer);}



Server script


var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc:function()


{return   gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'),false);}});




Screen shots are


Duration1.JPG    



Duration_2.JPG



Duration3.JPG


Durtion4.JPG



Duration5.JPG


Duration_Client Script.JPG



DurationScript_include.JPG



I am not sure where am I going wrong. Please help guyz to populate the Down Time here.



Thanks and   Regards,



Raju Singh