How to convert Duration to Seconds

devspider44
Giga Contributor

Is it possible to convert duration to seconds?

 

The real problem I have is this.

I have fields something like these on the form:

 

Start Date: (datetime field)

End Date:(datetime field)
Duration: (duration field)

 

On Duration change, I need to validate that the Duration field should not be longer than the duration between Start Date and End Date field. To do so when Duration field changes, I calculate duration between Start Date and End Date using the AjaxDurCalc script include:

 

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

 

and then compare it to the Duration field to check if the new value of Duration field is "lesser" than the in between duration of the Start and End dates.

 

There is no problem when the duration of Start and End date is more than a day.

 

For example:

 

Start Date: 21-07-2014 20:10:00
End Date: 22-07-2014 20:14:00
Duration: '2 00:00:00'

 

The output of AjaxDurCalc here is '1 00:04:00'. Then, if I evaluate (Duration > AjaxDurCalc), the output will be true since   '2 00:00:00' > '1 00:04:00'. If I change the Duration to '1 00:00:00', then the output will be false as expected since it is less than '1 00:04:00'.

 

The problem is when the AjaxDurCalc is less than '1 00:00:00', it evaluates wrongly when compared to a less than '1 00:00:00' Duration value.

Example, if I change the End Date from the previous example to the same date of Start Date and also set the Duration with 1 hour:

 

Start Date: 21-07-2014 20:10:00

End Date: 21-07-2014 20:14:00

Duration: '0 01:00:00'

 

AjaxDurCalc result is '00:04:00' (I noticed that the day value number is not present here)

 

I'm receiving a wrong result. If I evaluate '0 01:00:00' > '00:04:00' its giving me a false output which is not acceptable.

 

Am I missing something here? That's why I resorted if I can just convert both duration to seconds then compare it.

1 ACCEPTED SOLUTION

devspider44
Giga Contributor

Found a work around. Like my note on my question,
"AjaxDurCalc result is '00:04:00' (I noticed that the day value number is not present here)"


the value returned by AjaxDurCalc when the difference is less than a day does not include a zero to represent the day in it. I suspected that it is the reason for the buggy output. Therefore, I added condition to check if the difference is less than a day then add 0 on it. Something like this:



var duration = gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);


   


      if(duration < '1 00:00:00'){


                      duration = '0 ' + duration;


  }



return duration;



Now it it evaluates correctly is it returns '0 00:00:00' format.


View solution in original post

5 REPLIES 5

faisal_dadabhoy
Giga Expert

I used something like this previously, please test it ensure it is working for you.



var duration = current.u_duration.getGlideObject().getNumericValue();


var durationSeconds = (duration/1000);


gs.addInfoMessage(duration);


gs.addInfoMessage(durationSeconds);


Sorry meant to add another piece of code:



//setNumericValue(milliseconds)


var gdt = new GlideDateTime(current.u_datetime);


gs.addInfoMessage(gdt);


gs.addInfoMessage(gdt.getNumericValue());


Thanks for the idea. What I really need should happen in the client script though.


devspider44
Giga Contributor

Found a work around. Like my note on my question,
"AjaxDurCalc result is '00:04:00' (I noticed that the day value number is not present here)"


the value returned by AjaxDurCalc when the difference is less than a day does not include a zero to represent the day in it. I suspected that it is the reason for the buggy output. Therefore, I added condition to check if the difference is less than a day then add 0 on it. Something like this:



var duration = gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);


   


      if(duration < '1 00:00:00'){


                      duration = '0 ' + duration;


  }



return duration;



Now it it evaluates correctly is it returns '0 00:00:00' format.