Results 1 to 2 of 2

Thread: Need to calculate elapsed time in JS

  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to calculate elapsed time in JS

    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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •