View Full Version : Issue with javascript

12-08-2011, 10: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)) {
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){
day1 += dIM (year2, month2);day
if (month1 < month2) {
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 = "";

<form name = age>
<p>Day of birth&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" id="inputdate" size="2"><br/>
Month of birth&nbsp;
<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> You are
<input type = "text" name = "ageoutput" size = "4" value = "0"> years old
<input type = "text" name = "daysremaining" value = "0" />

12-09-2011, 07:17 AM
This has nothing to do with your question but <form> needs an action, name is deprecated, id is used instead, and the attributes should be in quotes.
So, <form name = age> S/B <form id="age" action="some action">.

A trouble shooting tip use alert('here');die; as the first line in a function to see if your getting there. In this case you are. Next trouble shooting tip Check your inputs. So I put alert(day);die; after var day = document.age.inputdate.value;
I got nothing. If I got an alert box I would know the data was wrong but nothing means the line(code) is wrong. Changed it to var day = document.getElementById('age').inputdate.value; Got the alert.:thumbsup:

12-09-2011, 09:16 PM
Thanks for the help, I did the corrections but unfortunately I still can't get it work, thanks anyways.

If anyone else can solve this, I'd be very grateful :D

12-10-2011, 04:36 PM
You should post your new code

Philip M
12-10-2011, 06:59 PM
It works for me in IE9 and correctly calculates my age.

The with statement is very unreliable, is deprecated and generally discouraged, and may cause problems in other browsers.

Quizmaster: What word is used for the letter "B" in the phonetic alphabet?
Contestant: Bob

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum