Log in

View Full Version : New to JavaScript and need some help



Lexilou
12-11-2007, 04:50 PM
Hello all,

I am new to the boards and to JavaScript and I am hoping some of you can help me. I am trying to design a script that will calcuate the number of days between two dates. I already have the basics below but it is the actual equation that I am having problem with. Any help would be appreciated.

Here is the code I have so far.



<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function dateDiff(firstmonth, firstday, firstyear, secondmonth, secondday, secondyear)
{

alert ("There are " + days + " days between the dates");
}
// End -->
</script>
</HEAD>


<BODY>

<center>
<form name=dateform>
<table>
<tr><td>
<pre>
First Date: Month: <input type=text name=firstmonth value="" size=10 maxlength=10>
Day: <input type=text name=firstday value="" size=10 maxlength=10>
Year: <input type=text name=firstyear value="" size=10 maxlength=10>

Second Date: Month: <input type=text name=secondmonth value="" size=10 maxlength=10>
Day: <input type=text name=secondday value="" size=10 maxlength=10>
Year: <input type=text name=secondyear value="" size=10 maxlength=10>

<center><input type=button value="Calculate Difference in days!"
onclick="dateDiff(firstmonth.value, firstday.value, firstyear.value, secondmonth.value,
secondday.value, secondyear.value);">

</center>
</pre>
</td></tr>
</table>
</form>
</center>


Thank you,

LL

rodneykm
12-11-2007, 06:00 PM
I'm by no means a javascript guru but I wanted to throw this out there. I don't see in your function that you are doing any math. Maybe this snippet I found elsewhere might help you out.

var strDate1 = document.formName.fieldName.value
var strDate2 = document.formName.fieldName.value

datDate1= Date.parse(strDate1);
datDate2= Date.parse(strDate2);

datediff = ((datDate1-datDate2)/(24*60*60*1000))

Twey
12-11-2007, 06:25 PM
That's how it works. But, you haven't defined datDate1, datDate2, or datediff, and accessing form elements should be done as document.forms.formName.elements.fieldName to avoid possible conflicts.