g_form getValue Duration

stevejarman
Giga Guru

Can anyone tell me how to get the value from a Duration field in a useful format? I need to be able to grab the Duration field value and minus it from a Date/Time value (which I already have). I'm totally stumped at this point.

1 ACCEPTED SOLUTION

I was talking about reading the value. All good though - I've just written a small function to convert the value it returns to a millisecond value, which is much more useable. Maybe there's a built-in way to do it, but if there is, I couldn't find it.



function GlideFormDurationToMS(_duration)


{


      var totalMS = 0;



      var dayTimeSplit = _duration.split(" ");



      totalMS += Number(dayTimeSplit[0]) * 86400000; // DAYS



      var timeSplit = dayTimeSplit[1].split(":");



      totalMS += Number(timeSplit[0]) * 3600000; // HOURS


      totalMS += Number(timeSplit[1]) * 60000; // MINUTES


      totalMS += Number(timeSplit[2]) * 1000; // SECONDS



      return totalMS;


}


View solution in original post

6 REPLIES 6

Rahul Kathuria
Tera Expert

Hi Steve,


You might find this useful:



---------------------------------


Calculating a duration and setting that value using a client script


Here's an example of how to use the 'gs.dateDiff' function to return a value and populate it using a client script.




--Create an 'onChange' client script that includes the following code. You can modify this script if you need the calculation to happen in an 'onLoad' script or some other way.


function onChange(control, oldValue, newValue, isLoading) {


  var strt = g_form.getValue('<start_field>');


  var end = g_form.getValue('<end_field>');


  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('<duration_field>', answer);


}


--Create a system script include file called AjaxDurCalc that handles the request. It may be reused for other functions as well.


var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


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


}


});




5 Changing the Duration

I was talking about reading the value. All good though - I've just written a small function to convert the value it returns to a millisecond value, which is much more useable. Maybe there's a built-in way to do it, but if there is, I couldn't find it.



function GlideFormDurationToMS(_duration)


{


      var totalMS = 0;



      var dayTimeSplit = _duration.split(" ");



      totalMS += Number(dayTimeSplit[0]) * 86400000; // DAYS



      var timeSplit = dayTimeSplit[1].split(":");



      totalMS += Number(timeSplit[0]) * 3600000; // HOURS


      totalMS += Number(timeSplit[1]) * 60000; // MINUTES


      totalMS += Number(timeSplit[2]) * 1000; // SECONDS



      return totalMS;


}



I noticed that the above script assumes that the "day" portion of the Duration value is greater than zero, so I made a slight modification to account for it.



// Function to convert duration value to numerical value


function GlideFormDurationToMS(duration)  


{  


      var totalMS = 0;  


 


  // Days are greater than zero


  if (duration.indexOf(' ') > -1)


  {


  var dayTimeSplit = duration.split(" ");  


 


  totalMS += Number(dayTimeSplit[0]) * 86400000; // DAYS  




  var timeSplit = dayTimeSplit[1].split(":");  




  totalMS += Number(timeSplit[0]) * 3600000; // HOURS  


  totalMS += Number(timeSplit[1]) * 60000; // MINUTES  


  totalMS += Number(timeSplit[2]) * 1000; // SECONDS  


  }


  // Days are zero


  else


          {


  var timeSplitNoDays = duration.split(":");  




  totalMS += Number(timeSplitNoDays[0]) * 3600000; // HOURS  


  totalMS += Number(timeSplitNoDays[1]) * 60000; // MINUTES  


  totalMS += Number(timeSplitNoDays[2]) * 1000; // SECONDS  


  }


 


      return totalMS;  


}


I'd like to use this in a business rule, but I don't see where I call the specific duration field that I want to read.



Once I get a return value, how do I call the 'totalMS' variable so I can manipulate it?



My use case is to obtain the duration value from a duration field, convert it to an integer, and update the value of a different field.