gatew
12-08-2011, 09:11 PM
Hey everyone, I've got this slight problem with my code in javascript and I cant solve it to save my life and I don't really want to mess more things around incase it makes it worse.
For some reason, when I click "get age" it just doesnt run, but the if statements seem to be running, any help would be appreciated
<script type = "text/javascript">
function dIM(Y, M) {
with (new Date(Y, M, 1, 12)) {
setDate(0);
return getDate();
}
}
function dateDifference(birthdate1, birthdate2)
{
var year1 = birthdate1.getFullYear(), month1 = birthdate1.getMonth(), day1 = birthdate1.getDate(),
year2 = birthdate2.getFullYear(), month2 = birthdate2.getMonth(), day2 = birthdate2.getDate();
if (day1 < day2){
month1--;
day1 += dIM (year2, month2);day
};
if (month1 < month2) {
year1--;
month1 += 12;
}
return [year1 - year2, month1 - month2, day1 - day2];
}
function ageCalculator() {
var day = document.age.inputdate.value;
var month = (document.age.inputmonth.value - 1);
var year = document.age.inputyear.value;
var now = new Date();
thisday = now.getDate();
thismonth = now.getMonth();
thisyear = now.getFullYear();
var first = new Date(thisyear, thismonth - 1, thisday);
var second = new Date(year, month - 1, day);
var yourage;
var datediff;
if (day == "" || month == "" || year == "")
{
alert ("Please fill in all of the boxes before getting your age");
}
else if ((day != parseInt(day)) || (month != parseInt(month)) || (year != parseInt(year)))
{
alert ("Please only enter digits in the day, month or year boxes"); return false;
}
datediff = dateDifference(first,second);
if ((thismonth < month) || (thismonth == month & thisday<=day)) {thisyear--;}
yourage = thisyear-year;
var next = parseInt(year)+datediff[0]+1;
var difference = Date.UTC(next, month, day, 0, 0, 0) - Date.UTC(thisyear, thismonth, thisday, 0, 0, 0);
var daysleft = difference/1000/60/60/24;
document.age.daysremaining.value = daysleft+" days left for your next birthday";
document.age.ageoutput.value = yourage;
}
function clear(form){
form.Result.value = "";
}
</script>
<center>
<p>
<form name = age>
<p>Day of birth
<input type="text" id="inputdate" size="2"><br/>
Month of birth
<input type="text" id="inputmonth" size="2"><br/>
Year of birth
<input type="text" id="inputyear" size="4" ><br/>
<input name="button" type="button" id="button2" onClick="ageCalculator()" value = "Get Age"/>
<input type="reset" name="Reset" id="button" value="Reset" />
</p>
<p> You are
<input type = "text" name = "ageoutput" size = "4" value = "0"> years old
<input type = "text" name = "daysremaining" value = "0" />
</p>
</form>
For some reason, when I click "get age" it just doesnt run, but the if statements seem to be running, any help would be appreciated
<script type = "text/javascript">
function dIM(Y, M) {
with (new Date(Y, M, 1, 12)) {
setDate(0);
return getDate();
}
}
function dateDifference(birthdate1, birthdate2)
{
var year1 = birthdate1.getFullYear(), month1 = birthdate1.getMonth(), day1 = birthdate1.getDate(),
year2 = birthdate2.getFullYear(), month2 = birthdate2.getMonth(), day2 = birthdate2.getDate();
if (day1 < day2){
month1--;
day1 += dIM (year2, month2);day
};
if (month1 < month2) {
year1--;
month1 += 12;
}
return [year1 - year2, month1 - month2, day1 - day2];
}
function ageCalculator() {
var day = document.age.inputdate.value;
var month = (document.age.inputmonth.value - 1);
var year = document.age.inputyear.value;
var now = new Date();
thisday = now.getDate();
thismonth = now.getMonth();
thisyear = now.getFullYear();
var first = new Date(thisyear, thismonth - 1, thisday);
var second = new Date(year, month - 1, day);
var yourage;
var datediff;
if (day == "" || month == "" || year == "")
{
alert ("Please fill in all of the boxes before getting your age");
}
else if ((day != parseInt(day)) || (month != parseInt(month)) || (year != parseInt(year)))
{
alert ("Please only enter digits in the day, month or year boxes"); return false;
}
datediff = dateDifference(first,second);
if ((thismonth < month) || (thismonth == month & thisday<=day)) {thisyear--;}
yourage = thisyear-year;
var next = parseInt(year)+datediff[0]+1;
var difference = Date.UTC(next, month, day, 0, 0, 0) - Date.UTC(thisyear, thismonth, thisday, 0, 0, 0);
var daysleft = difference/1000/60/60/24;
document.age.daysremaining.value = daysleft+" days left for your next birthday";
document.age.ageoutput.value = yourage;
}
function clear(form){
form.Result.value = "";
}
</script>
<center>
<p>
<form name = age>
<p>Day of birth
<input type="text" id="inputdate" size="2"><br/>
Month of birth
<input type="text" id="inputmonth" size="2"><br/>
Year of birth
<input type="text" id="inputyear" size="4" ><br/>
<input name="button" type="button" id="button2" onClick="ageCalculator()" value = "Get Age"/>
<input type="reset" name="Reset" id="button" value="Reset" />
</p>
<p> You are
<input type = "text" name = "ageoutput" size = "4" value = "0"> years old
<input type = "text" name = "daysremaining" value = "0" />
</p>
</form>