Getting an NaN after calculation...

# Thread: Getting an NaN after calculation...

1. Senior Member
Join Date
Dec 1969
Posts
389

## Getting an NaN after calculation...

Bill gave me a function to use to add up fields on my form and display them in another. It works great if all 17 fields are filled in. But if one is left blank, the value entered into the display field is NaN. How can I insert a 0 into the blank ones using the following function?<BR><BR>function calcTotal( ) <BR>{ <BR> var frm = document.frmTmSht; <BR> var total = 0; <BR> for ( var tnum = 1; tnum &#060;= 17; ++ tnum ) <BR> { <BR> total += parseInt( frm.elements["txt_TotalTime"+tnum].value ); <BR> } <BR> frm.txt_Total.value = total; <BR>}

2. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Getting an NaN after calculation...

total += (IsNaN(parseInt(frm.elements["txt_TotalTime"+tnum].value) ? 0 : parseInt(frm.elements["txt_TotalTime"+tnum].value));<BR><BR>Or something very similar.<BR><BR>Craig.

3. Senior Member
Join Date
Dec 1969
Posts
389

## Reaper Man, I have a different question for you...

I have on my form a time field. There are actually 17 of it, and i build them using a loop. So I used txt_StartTimex, where x get incremented on each loop. So I have txt_StartTime1, txt_StartTime2, etc. Here is my question. I want to have a onclick expression, where when a user clicks on that field, the current time is filled in. My question is how do I know which of my txt_StartTimex should be filled in?

4. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Reaper Man, I have a different question for yo

If you pass a reference to the object into your onClick event handler...<BR><BR>&#060;script&#062;<BR>function handler(oObject){<BR> // oObject now contains the txt_StartTimeX input element<BR>}<BR>&#060;/script&#062;<BR>&#060;input type="text" name="txt_StartTime1" onclick="handler(oObject)" /&#062;<BR>&#060;input type="text" name="txt_StartTime2" onclick="handler(oObject)" /&#062;<BR><BR>Does that help any?<BR><BR>Craig.

5. Senior Member
Join Date
Dec 1969
Posts
16,931

## I'm an idiot

&#060;script&#062;<BR>function handler(oObject){<BR> // oObject now contains the txt_StartTimeX input element<BR>}<BR>&#060;/script&#062;<BR>&#060;input type="text" name="txt_StartTime1" onclick="handler(this)" /&#062;<BR>&#060;input type="text" name="txt_StartTime2" onclick="handler(this)" /&#062;<BR><BR>Note "this" in the onclick.<BR><BR>Craig.

6. Senior Member
Join Date
Dec 1969
Posts
389

## Added my time function, does this look correct...

&#060;script&#062; <BR>function GetTime(oObject){ <BR>var frm = document.frmTmSht;<BR>var nowDate = new Date();<BR>var nowHour = nowDate.getHours();<BR>var nowMinute = nowDate.getMinutes();<BR>var nowSecond = nowDate.getSeconds();<BR><BR>if (nowMinute &#060; 10)<BR>{<BR> nowMinute = "0" + nowMinute;<BR>}<BR><BR>if (nowSecond &#060; 10)<BR>{ <BR> nowSecond = "0" + nowSecond;<BR>}<BR><BR>frm.oObject = (nowHour + ":" + nowMinute + ":" + nowSecond);<BR>}<BR><BR><BR><BR><BR><BR><BR>&#060; input type="text" name="txt_StartTime1" onclick="GetTime(this)" /&#062; <BR>&#060;input type="text" name="txt_StartTime2" onclick="GetTime(this)" /&#062;

7. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Added my time function, does this look correct

Simpler than that. oObject contains the actual reference to the object. You don&#039;t need "frm.", or even that form reference.<BR><BR>&#060;script&#062;<BR>function GetTime(oObject){<BR> var nowDate = new Date();<BR> var nowHour = nowDate.getHours();<BR> var nowMinute = nowDate.getMinutes();<BR> var nowSecond = nowDate.getSeconds();<BR> if (nowMinute &#060; 10){ nowMinute = "0" + nowMinute; }<BR> if (nowSecond &#060; 10){ nowSecond = "0" + nowSecond; }<BR> oObject.value = (nowHour + ":" + nowMinute + ":" + nowSecond);<BR>}<BR>&#060;/script&#062;<BR><BR>See? oObject is the actual input element. All you need to do is set its .value property to the string you&#039;re building up and you&#039;re laughing.<BR><BR>Craig.

8. Senior Member
Join Date
Dec 1969
Posts
389

## Worked great, time for one more?...

Thank you for all the help, it is greatly appreciated. I have one more thing I need to get to work, then I promise I won&#039;t bug you anymore (hopefully anyways). So I have a start time and an end time. Then I have a time colum where I need to figure out how much time has passed between the start time and the end time and put it in a decimal format. For example:<BR><BR>start time: 9:35<BR>End time: 14:25<BR>Total time: 4.83 hours<BR><BR>Is there a function out there that already does this, or any ideas how I shuld go about writing it?

9. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Worked great, time for one more?...

There&#039;s no built-in function.<BR><BR>Best thing would be to create two Date objects in memory (give them both the same actual date, but different times). You can then find the millisecond difference between the two:<BR>var iDiff = myDate1.getTime() - myDate2.getTime();<BR><BR>Then converting that to an hour-difference will be a matter of dividing it correctly by 1000 (seconds) and then by 60 (minutes) and then by 60 again (hours).<BR><BR>Sorry there&#039;s no easy way, but if you follow those steps it shouldn&#039;t be too hard.<BR><BR>Craig.

10. Senior Member
Join Date
Dec 1969
Posts
389

## RE: Worked great, time for one more?...

Actually I wanted to use the 2 time fields from the previous problem I was having.

#### Posting Permissions

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