PDA

View Full Version : Need to calculate elapsed time in JS



bklerner
03-25-2009, 11:28 PM
Hello,

I'm working on a form in Acrobat Pro 9 (not LiveCycle). I need to have an operator enter a beginning time (firsttime) & an end time (secondtime) & when s/he pushes a button (btncalcmix.0), the form calculates the total elapsed time. It must take into consideration that a test could begin at 4 pm & end at 8 am the next day. This is what I've put together so far.....but it doesn't work. I get this message: syntax error 10: at line 11.


// if button pushed, calculate mix time

var f=this.getField("btncalcmix.0");
this.getField ("ttlmix").value = this.getField("dateform.secondtime").value - this.getField("dateform.firsttime").value;

if ((this.getField("dateform.secondtime").value)="" || (this.getField("dateform.firsttime").value="")); {
this.getField("ttlmix").value = 0;
}

if((this.getField("dateform.secondtime").value) <- this.getField("dateform.firsttime").value)) {
((this.getField ("ttlmix").value)*24)+24;
} else {
this.getField("ttlmix").value)*24));
}

if (f.value!= "true") {
this.getField("ttlmix")=util.printd("HH:MM:ss", new Date());
}
Any suggestions? Thank you in advance.

jscheuer1
03-26-2009, 02:28 AM
I'm not following you at the moment. In javascript there is no way to calculate times except on the current page. If someone leaves and comes back later, the best you can do is set a cookie and start counting again when they get back, but this is fairly unreliable.

Consider the following:


<script type="text/javascript">
var timeA = new Date().getTime(), timeB;
setTimeout(function(){
timeB = new Date().getTime();
alert('milliseconds elapsed is ' + (timeB - timeA));
}, 1000);
</script>

It should always be 1000, but it will vary by browser, CPU, etc.

Times in javascript are approximate. However, if you set a date with data and another date with other data and subtract one from the other, the result will be the difference between the two.

See:

http://www.w3schools.com/jS/js_obj_date.asp

and:

http://www.w3schools.com/jsref/jsref_obj_date.asp