Clock In/Out Time total function

# Thread: Clock In/Out Time total function

1. Senior Member
Join Date
Dec 1969
Posts
246

## Clock In/Out Time total function

I have a form that allows the user to input time in/out up to 4 pairs per day.<BR>In1 - Out1<BR>In2 - Out2<BR>In3 - Out3<BR>In4 - Out4<BR><BR>If there is only one pair of entries and it&#039;s total is over 5 hours then .5 of an hour is taken out. If there is more than one pair then I check to make sure that at least one occurance of a difference of .5 hours or more between a clock out and the next clock in. If there is not then I take the difference of the In2-Out1 and subtract that from .5. I take that number and subtract it from the total and add it to the first Out (Out1). If you understood all of that...wow...and maybe you wouldn&#039;t mind taking a look at my code.<BR><BR>function calctime() <BR>{ <BR> var totalhours = 0; <BR> var frm = document.timecard; <BR> for ( var i=1; i &#060;= 4; i++) <BR> { <BR> var val = parseFloat( frm.elements["Out"+i].value - frm.elements["In"+i].value ); <BR> if ( ! isNaN(val) ) totalhours += val; <BR> } <BR> var reglunch = 0.5<BR> if ( totalhours &#062; 5 ) {<BR> if ( frm.elements["In2"].value == "" && frm.elements["In3"].value == "" && frm.elements["In4"].value == "" ) {<BR> totalhours -= reglunch;<BR> }<BR> else {<BR> var countdiff = 0;<BR> arrTimeDiff = new Array;<BR> for (var x=2; x &#060;= 4; x++ ) {<BR> arrTimeDiff[x] = parseFloat( frm.elements["In"+x].value - frm.elements["Out"+(x-1)].value );<BR> if ( ! arrTimeDiff[x] &#062;= reglunch ) countdiff += 1;<BR> }<BR> if ( countdiff &#062; 0 ) {<BR> frm.Out1.value = parseFloat( frm.Out1.value + (reglunch - arrTimeDiff[2]) );<BR> totalhours = parseFloat(totalhours - (reglunch - arrTimeDiff[2]));<BR> }<BR> }<BR> }<BR> frm.totaltime.value = Math.round(totalhours*10)/10; <BR>} <BR><BR>In some cases I get a good number. If there is more than one pair of entries then no matter what I can&#039;t get the "Out1" to change. The total hours will change. There is also a case when the code seems to ignore the 2nd In "In2" when calculating the total. I am a newbie to javascript so be easy with me. I created 75% of this script on my own and I&#039;m sure it shows. Oh...the function is called by an onChange on each of the in/out fields.

2. Senior Member
Join Date
Dec 1969
Posts
635

## Does this help ? [Big post warning]

I&#039;ve created this little test page.<BR><BR>I&#039;ve also rewritten your function so that my tiny head can understand it. As far as my testing goes, it seems to work fine but you might want to review it :)<BR><BR>Eniac<BR><BR>[WARNING : Big post ahead]<BR><BR>&#060;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&#062;<BR><BR>&#060;html&#062;<BR>&#060;head&#0 62;<BR> &#060;title&#062;Untitled&#060;/title&#062;<BR>&#060;script&#062;<BR><BR>function SetClockTime(prmobjTarget) {<BR>var currentTime = new Date()<BR> prmobjTarget.value = currentTime.getHours() + ":" + currentTime.getMinutes()<BR>}<BR><BR>function Time2Float(strValue) {<BR>//Set a default splitter is none is passed<BR>var strSplitter = ((arguments[1] != null) ? arguments[1] : ":")<BR>var arrValue = strValue.toString().split(strSplitter)<BR>var flValue = parseFloat(arrValue[0] + "." + (parseInt(arrValue[1], 10) * 100/60))<BR><BR>return ( !isNaN(flValue) ? flValue : 0)<BR>}<BR><BR>function CalcTime2() {<BR>var f = document.forms[&#039;timecard&#039;]<BR>var in1 = Time2Float(f.txtIN1.value)<BR>var in2 = Time2Float(f.txtIN2.value)<BR>var in3 = Time2Float(f.txtIN3.value)<BR>var in4 = Time2Float(f.txtIN4.value)<BR><BR>var out1 = Time2Float(f.txtOUT1.value)<BR>var out2 = Time2Float(f.txtOUT2.value)<BR>var out3 = Time2Float(f.txtOUT3.value)<BR>var out4 = Time2Float(f.txtOUT4.value)<BR><BR>var total1 = ((total1 = out1 - in1) &#062; 0 ? total1 : 0)<BR>var total2 = ((total2 = out2 - in2) &#062; 0 ? total2 : 0)<BR>var total3 = ((total3 = out3 - in3) &#062; 0 ? total3 : 0)<BR>var total4 = ((total4 = out4 - in4) &#062; 0 ? total4 : 0)<BR>var total234 = total2 + total3 + total4<BR><BR>var flTmp<BR><BR>var isOK<BR><BR><BR>//Now that we got all the values, process them<BR>if (total234 = 0) {<BR> //There&#039;s only one entry<BR> if (total1 &#062;= 5) {total1 -= .5}<BR>} else {<BR> //There&#039;s more than one entry<BR> isOK = (in2 - out1) &#062;= .5 //Check time diff between In2 and Out1<BR> if (!isOK) {<BR> isOK = (in3 - out2 &#062;= .5) //Check time diff between In3 and Out2<BR> if (!isOK) {<BR> isOK = (in4 - out3 &#062;= .5) //Check time diff between In4 and Out3<BR> }<BR> }<BR> <BR> //No time entry has a difference of .5 (30 minutes)<BR> if (!isOK) {<BR> flTmp = .5 - (in2 - out1)<BR> out1 += flTmp<BR> } else {<BR> flTmp = 0<BR> }<BR> <BR>}<BR><BR>var grandTotal = (total1 + total234) - flTmp<BR><BR>f.txtTotal1.value = total1<BR>f.txtTotal2.value = total2<BR>f.txtTotal3.value = total3<BR>f.txtTotal4.value = total4<BR><BR>f.txtBigTotal.value = Math.round(grandTotal*10)/10<BR><BR><BR>}<BR><BR>function calctime() <BR>{ <BR> var totalhours = 0; <BR> var frm = document.timecard; <BR> for ( var i=1; i &#060;= 4; i++) <BR> { <BR> var val = parseFloat( frm.elements["Out"+i].value - frm.elements["In"+i].value ); <BR> if ( ! isNaN(val) ) totalhours += val; <BR> } <BR> var reglunch = 0.5 <BR> if ( totalhours &#062; 5 ) { <BR> if ( frm.elements["In2"].value == "" && frm.elements["In3"].value == "" && frm.elements["In4"].value == "" ) { <BR> totalhours -= reglunch; <BR> } <BR> else { <BR> var countdiff = 0; <BR> arrTimeDiff = new Array; <BR> for (var x=2; x &#060;= 4; x++ ) { <BR> arrTimeDiff[x] = parseFloat( frm.elements["In"+x].value - frm.elements["Out"+(x-1)].value ); <BR> if ( ! arrTimeDiff[x] &#062;= reglunch ) countdiff += 1; <BR> } <BR> if ( countdiff &#062; 0 ) { <BR> frm.Out1.value = parseFloat( frm.Out1.value + (reglunch - arrTimeDiff[2]) ); <BR> totalhours = parseFloat(totalhours - (reglunch - arrTimeDiff[2])); <BR> } <BR> } <BR> } <BR> frm.totaltime.value = Math.round(totalhours*10)/10; <BR>} <BR>&#060;/script&#062;<BR>&#060;/head&#062;<BR><BR>&#060;body&#062;<BR>&#060;pre&#0 62;<BR>Clock In/Out Time total function<BR>reverend - 3/14/2003 9:39:54 AM<BR><BR>--------------------------------------------------------------------------------<BR> I have a form that allows the user to input time in/out up to 4 pairs per day. <BR>In1 - Out1 <BR>In2 - Out2 <BR>In3 - Out3 <BR>In4 - Out4 <BR><BR>If there is only one pair of entries and it&#039;s total is over 5 hours then .5 of an hour is taken out.<BR><BR>If there is more than one pair then I check to make sure that at least one occurance <BR>of a difference of .5 hours or more between a clock out and the next clock in.<BR><BR>If there is not then I take the difference of the In2-Out1 and subtract that from .5.<BR>I take that number and subtract it from the total and add it to the first Out (Out1).<BR><BR>If you understood all of that...wow...and maybe you wouldn&#039;t mind taking a look at my code. <BR>&#060;/pre&#062;<BR>&#060;form name="timecard"&#062;<BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 1&nbsp;&#060;input type="text" value="" name="txtIN1"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN1)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 1&nbsp;&#060;input type="text" value="" name="txtOUT1"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT1)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 2&nbsp;&#060;input type="text" value="" name="txtIN2"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN2)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 2&nbsp;&#060;input type="text" value="" name="txtOUT2"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT2)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 3&nbsp;&#060;input type="text" value="" name="txtIN3"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN3)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 3&nbsp;&#060;input type="text" value="" name="txtOUT3"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT3)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 4&nbsp;&#060;input type="text" value="" name="txtIN4"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN4)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 4&nbsp;&#060;input type="text" value="" name="txtOUT4"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT4)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td width="25%"&#062;Total (1)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (2)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (3)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (4)&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal1" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal2" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal3" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal4" value=""&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td colspan="2" align="right"&#062;&#060;input type="button" value="Calculate" OnClick="CalcTime2()"&#062;&#060;/td&#062;<BR> &#060;td&#062;Grand Total&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtBigTotal" value=""&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR>&#060;/form&#062;<BR><BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR>

3. Senior Member
Join Date
Dec 1969
Posts
635

## Does this help ? [Big post warning]

I&#039;ve created this little test page.<BR><BR>I&#039;ve also rewritten your function so that my tiny head can understand it. As far as my testing goes, it seems to work fine but you might want to review it :)<BR><BR>Eniac<BR><BR>[WARNING : Big post ahead]<BR><BR>&#060;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&#062;<BR><BR>&#060;html&#062;<BR>&#060;head&#0 62;<BR> &#060;title&#062;Untitled&#060;/title&#062;<BR>&#060;script&#062;<BR><BR>function SetClockTime(prmobjTarget) {<BR>var currentTime = new Date()<BR> prmobjTarget.value = currentTime.getHours() + ":" + currentTime.getMinutes()<BR>}<BR><BR>function Time2Float(strValue) {<BR>//Set a default splitter is none is passed<BR>var strSplitter = ((arguments[1] != null) ? arguments[1] : ":")<BR>var arrValue = strValue.toString().split(strSplitter)<BR>var flValue = parseFloat(arrValue[0] + "." + (parseInt(arrValue[1], 10) * 100/60))<BR><BR>return ( !isNaN(flValue) ? flValue : 0)<BR>}<BR><BR>function CalcTime2() {<BR>var f = document.forms[&#039;timecard&#039;]<BR>var in1 = Time2Float(f.txtIN1.value)<BR>var in2 = Time2Float(f.txtIN2.value)<BR>var in3 = Time2Float(f.txtIN3.value)<BR>var in4 = Time2Float(f.txtIN4.value)<BR><BR>var out1 = Time2Float(f.txtOUT1.value)<BR>var out2 = Time2Float(f.txtOUT2.value)<BR>var out3 = Time2Float(f.txtOUT3.value)<BR>var out4 = Time2Float(f.txtOUT4.value)<BR><BR>var total1 = ((total1 = out1 - in1) &#062; 0 ? total1 : 0)<BR>var total2 = ((total2 = out2 - in2) &#062; 0 ? total2 : 0)<BR>var total3 = ((total3 = out3 - in3) &#062; 0 ? total3 : 0)<BR>var total4 = ((total4 = out4 - in4) &#062; 0 ? total4 : 0)<BR>var total234 = total2 + total3 + total4<BR><BR>var flTmp<BR><BR>var isOK<BR><BR><BR>//Now that we got all the values, process them<BR>if (total234 = 0) {<BR> //There&#039;s only one entry<BR> if (total1 &#062;= 5) {total1 -= .5}<BR>} else {<BR> //There&#039;s more than one entry<BR> isOK = (in2 - out1) &#062;= .5 //Check time diff between In2 and Out1<BR> if (!isOK) {<BR> isOK = (in3 - out2 &#062;= .5) //Check time diff between In3 and Out2<BR> if (!isOK) {<BR> isOK = (in4 - out3 &#062;= .5) //Check time diff between In4 and Out3<BR> }<BR> }<BR> <BR> //No time entry has a difference of .5 (30 minutes)<BR> if (!isOK) {<BR> flTmp = .5 - (in2 - out1)<BR> out1 += flTmp<BR> } else {<BR> flTmp = 0<BR> }<BR> <BR>}<BR><BR>var grandTotal = (total1 + total234) - flTmp<BR><BR>f.txtTotal1.value = total1<BR>f.txtTotal2.value = total2<BR>f.txtTotal3.value = total3<BR>f.txtTotal4.value = total4<BR><BR>f.txtBigTotal.value = Math.round(grandTotal*10)/10<BR><BR><BR>}<BR><BR>function calctime() <BR>{ <BR> var totalhours = 0; <BR> var frm = document.timecard; <BR> for ( var i=1; i &#060;= 4; i++) <BR> { <BR> var val = parseFloat( frm.elements["Out"+i].value - frm.elements["In"+i].value ); <BR> if ( ! isNaN(val) ) totalhours += val; <BR> } <BR> var reglunch = 0.5 <BR> if ( totalhours &#062; 5 ) { <BR> if ( frm.elements["In2"].value == "" && frm.elements["In3"].value == "" && frm.elements["In4"].value == "" ) { <BR> totalhours -= reglunch; <BR> } <BR> else { <BR> var countdiff = 0; <BR> arrTimeDiff = new Array; <BR> for (var x=2; x &#060;= 4; x++ ) { <BR> arrTimeDiff[x] = parseFloat( frm.elements["In"+x].value - frm.elements["Out"+(x-1)].value ); <BR> if ( ! arrTimeDiff[x] &#062;= reglunch ) countdiff += 1; <BR> } <BR> if ( countdiff &#062; 0 ) { <BR> frm.Out1.value = parseFloat( frm.Out1.value + (reglunch - arrTimeDiff[2]) ); <BR> totalhours = parseFloat(totalhours - (reglunch - arrTimeDiff[2])); <BR> } <BR> } <BR> } <BR> frm.totaltime.value = Math.round(totalhours*10)/10; <BR>} <BR>&#060;/script&#062;<BR>&#060;/head&#062;<BR><BR>&#060;body&#062;<BR>&#060;pre&#0 62;<BR>Clock In/Out Time total function<BR>reverend - 3/14/2003 9:39:54 AM<BR><BR>--------------------------------------------------------------------------------<BR> I have a form that allows the user to input time in/out up to 4 pairs per day. <BR>In1 - Out1 <BR>In2 - Out2 <BR>In3 - Out3 <BR>In4 - Out4 <BR><BR>If there is only one pair of entries and it&#039;s total is over 5 hours then .5 of an hour is taken out.<BR><BR>If there is more than one pair then I check to make sure that at least one occurance <BR>of a difference of .5 hours or more between a clock out and the next clock in.<BR><BR>If there is not then I take the difference of the In2-Out1 and subtract that from .5.<BR>I take that number and subtract it from the total and add it to the first Out (Out1).<BR><BR>If you understood all of that...wow...and maybe you wouldn&#039;t mind taking a look at my code. <BR>&#060;/pre&#062;<BR>&#060;form name="timecard"&#062;<BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 1&nbsp;&#060;input type="text" value="" name="txtIN1"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN1)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 1&nbsp;&#060;input type="text" value="" name="txtOUT1"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT1)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 2&nbsp;&#060;input type="text" value="" name="txtIN2"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN2)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 2&nbsp;&#060;input type="text" value="" name="txtOUT2"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT2)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 3&nbsp;&#060;input type="text" value="" name="txtIN3"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN3)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 3&nbsp;&#060;input type="text" value="" name="txtOUT3"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT3)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr&#062;<BR> &#060;td width="50%"&#062;In 4&nbsp;&#060;input type="text" value="" name="txtIN4"&#062; &nbsp;&#060;input type="button" value="Clock IN" Onclick="SetClockTime(form.txtIN4)"&#062;&#060;/td&#062;<BR> &#060;td width="50%"&#062;Out 4&nbsp;&#060;input type="text" value="" name="txtOUT4"&#062; &nbsp;&#060;input type="button" value="Clock OUT" Onclick="SetClockTime(form.txtOUT4)"&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR><BR>&#060;table width="650" border="1"&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td width="25%"&#062;Total (1)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (2)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (3)&#060;/td&#062;<BR> &#060;td width="25%"&#062;Total (4)&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal1" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal2" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal3" value=""&#062;&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtTotal4" value=""&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr align="center"&#062;<BR> &#060;td colspan="2" align="right"&#062;&#060;input type="button" value="Calculate" OnClick="CalcTime2()"&#062;&#060;/td&#062;<BR> &#060;td&#062;Grand Total&#060;/td&#062;<BR> &#060;td&#062;&#060;input type="text" name="txtBigTotal" value=""&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/table&#062;<BR>&#060;/form&#062;<BR><BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR>

4. Senior Member
Join Date
Dec 1969
Posts
635

## I'm a click happy person, don't mind thi

.

5. Senior Member
Join Date
Dec 1969
Posts
11,247

## just send small files when click happy (eop)

.

6. Senior Member
Join Date
Dec 1969
Posts
635

## LOL, truth is....

Actually, I got some firewall error when I first submitted the post, so I thought it didn&#039;t work.<BR><BR>I clicked back and submitted again. I only realized it worked the first time when I came back on the JScript forum page.<BR><BR>But, ok, I swear, if I evee go in click happy mode again, I&#039;ll post small text, not an entire HTML page :)<BR><BR>Eniac

7. Senior Member
Join Date
Dec 1969
Posts
635

## DoH! a bug

this line :<BR><BR>if (total234 = 0) { <BR><BR><BR>should read :<BR><BR>if (total234 == 0) { <BR><BR>sorry about that :)

8. Senior Member
Join Date
Dec 1969
Posts
246

## The most important truth is...

it works! It took a little tweaking but as far as I can tell it works. The time entered on these is not regular time. 7:30am is 7.50 and 5:30pm is 15.50 and so on...<BR><BR>Thanks!

#### Posting Permissions

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