PDA

View Full Version : Simple mathematicall formula???


njwoods
03-17-2006, 01:30 PM
I have just begun learning javascript and could use some real help. I am looking for a way to subtract two "times", for use in an Acrobat form.

e.g: (11:00-08:00)+(21:45-11:30)+(23:00-21:45)=total time

OR (in field names)

(time2-time1)+(time4-time3)=totaltime1


any assistance is greatly appreciated

liorean
03-17-2006, 01:38 PM
You could use the Date object for it. See <uri:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Date> for data about that.

swtrans
03-17-2006, 02:02 PM
I'm glad njwoods asked this question. Was just about to ask about time differences and there was a good example in the link from liorean.

I'm having problems using a function to update the time. Am I missing something simple or does this have to do with the date/time requirements?

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

<script type="text/javascript">

var timeA = new Date();
var timeB = new Date();
var timeDif = timeB - timeA;

function UpdateTimeB(){
timeB = new Date();
}

</script>

<input type="button" value="timeA" onclick="alert(timeA)"><br><br>
<input type="button" value="timeB" onclick="alert(timeB)"><br><br>
<input type="button" value="timeDif" onclick="alert(timeDif)"><br><br>
<input type="button" value="UpdateTimeB" onclick="updateTimeB()"><br><br>

liorean
03-17-2006, 02:11 PM
swtrans:
You did the timeDif=timeB-timeA part once, with the original values for timeA and timeB. You never after that update timeDif, so it will stay the same. Only timeB will be updated.

swtrans
03-17-2006, 03:38 PM
liorean see what you mean. I added an update to that, but for some reason I keep getting an error when I push the update button.

<script type="text/javascript">

var timeA = new Date();
var timeB = new Date();
var timeDif = timeB - timeA;

function UpdateTimeB(){
timeB = new Date();
timeDif = timeB - timeA;
}

</script>

<input type="button" value="timeA" onclick="alert(timeA)"><br><br>
<input type="button" value="timeB" onclick="alert(timeB)"><br><br>
<input type="button" value="timeDif" onclick="alert(timeDif)"><br><br>
<input type="button" value="UpdateTimeB" onclick="updateTimeB()"><br><br>

swtrans
03-17-2006, 03:40 PM
I see the problem:

onclick="updateTimeB()"

case sensative

onclick="UpdateTimeB()"><