...

View Full Version : calculate the age



awesomest
02-25-2012, 05:55 AM
hi, I've got a problem and i really need help.
this is my code

<script>
var y= new Date();
y.getFullYear() + '<br />';
alert(y);
var year = new Date();
year.setFullYear(prompt('Enter the year','1990'),prompt('Enter the month','1'), prompt('Enter the day','1'));
alert(year);
var yy = y-year;
alert(yy);
</script>

i want when a user write his/her birthday JS calculate the age. that's all

Philip M
02-25-2012, 08:43 AM
Not as easy as that, as need to check entered date is valid (not 31st February) and whether or not birthday has been reached this year. You cannot simply subtract the birth year from the current year.


Enter Date Of Birth in YYYY-MM-DD format <input type = "text" name = "dob" id = "dob" size = "10" maxlength = "10" onblur = "checkAge()">

<script type="text/javascript">

function checkAge(){
var today = new Date();
var d = document.getElementById("dob").value;
if (!/\d{4}\-\d{2}\-\d{2}/.test(d)) { // check valid format
showMessage();
return false;
}

d = d.split("-");
var byr = parseInt(d[0]);
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900) { // check valid year
showMessage();
return false;
}
var bmth = parseInt(d[1],10)-1; // radix 10!
if (bmth <0 || bmth >11) { // check valid month 0-11
showMessage()
return false;
}
var bdy = parseInt(d[2],10); // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim) { // check valid date according to month
showMessage();
return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
if (bmth > nowmonth) {age = age - 1} // next birthday not yet reached
else if (bmth == nowmonth && nowday < bdy) {age = age - 1}

alert('You are ' + age + ' years old');
if (age <= 15) {
alert ("You are 15 years old or less!");
}
}

function showMessage() {
if (document.getElementById("dob").value != "") {
alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
document.getElementById("dob").value = "";
document.getElementById("dob").focus();
}
}

function daysInMonth(month,year) { // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}

</script>

As this is probably homework here is a simpler script which performs minimal error checking (Garbage In, Garbage Out!):-


<script type = "text/javascript">

var today = new Date();
var nowyr = today.getFullYear();
var dob = prompt("Enter your age in YYYY/MM/DD format","")
var dobsplit = dob.split("/");
var byr = dobsplit[0];
var bmth = dobsplit[1];
var bday = dobsplit[2];
var age = nowyr-byr;
var d = new Date(nowyr,bmth-1,bday); // months in Javascript are 0-11
if (d > today) { // birthday not yet reached
age --; so subtract 1
}

if ((isNaN(age)) || (age < 0)){
alert ("Invalid data!");
}

else {alert ("Your age is " + age);
}

</script>

Quizmaster: In which century was Coca Cola invented?
Contestant: The eighteenth

siberia-man
02-25-2012, 01:23 PM
var birthdate = prompt("Enter your age in YYYY/MM/DD format","");

var b = new Date(birthdate);
if ( isNaN(b) ) {

alert('Invalid age format');

} else {

var now = new Date();

var age = now.getFullYear() - b.getFullYear();
if ( now.getMonth() < b.getMonth() ) {
age--;
}

alert('Age is ' + age);

}

Philip M
02-25-2012, 01:28 PM
siberia-man - you must take account of the birth day, not just the birth month!

siberia-man
02-25-2012, 04:26 PM
Philip M, yes! Your are totally right. I forgot this.

So. To allow the homeworker to stress his brain by himself to find which line he should fix I just show the part of the corrected line here :-)


now.getMonth() - b.getMonth() < 0 || now.getDate() - b.getDate() < 0

Philip M
02-25-2012, 04:41 PM
Philip M, yes! Your are totally right. I forgot this.

So. To allow the homeworker to stress his brain by himself to find which line he should fix I just show the part of the corrected line here :-)


now.getMonth() - b.getMonth() < 0 || now.getDate() - b.getDate() < 0


Sorry, still wrong! Your brain has frozen! Try entering 2000/3/3 and you get age 11, not 12.


<script type = "text/javascript">

var birthdate = prompt ("Enter your age in YYYY/MM/DD format","");

var bdate = new Date(birthdate);
if ( isNaN(bdate) ) {
alert ('Invalid age format');
}
else {
var now = new Date();
var age = now.getFullYear() - bdate.getFullYear();
var mth = now.getMonth();
var bmth = bdate.getMonth();
if (mth - bmth <0 ) {age--}
else if ((mth-bmth== 0) && (now.getDate() - bdate.getDate() < 0)) {age--}
alert('Age is ' + age);

}
</script>

siberia-man
02-25-2012, 11:57 PM
... Try entering 2000/3/3 and you get age 11, not 12. ...

It is very strange and it has interested me how did you got this result. I especially have tested this in several environments such as Firefox, MSIE, Iron (Chromium), WSH, Rhino, NodeJS. All of them have shown the result 11.

I just want to say that age 11 in this case is correct value.

Philip M
02-26-2012, 09:00 AM
It is very strange and it has interested me how did you got this result. I especially have tested this in several environments such as Firefox, MSIE, Iron (Chromium), WSH, Rhino, NodeJS. All of them have shown the result 11.

I just want to say that age 11 in this case is correct value.

Whoops! :o My bad!

siberia-man
02-26-2012, 02:48 PM
After your words I thught that I met that case when engine tries to execute the whole condition before making the next step. But I remember JS is "lazy" and does not execute the rest of the condition if the result is known. So I began to test in all ebvironments available for me.

awesomest
02-26-2012, 05:04 PM
Sorry, still wrong! Your brain has frozen! Try entering 2000/3/3 and you get age 11, not 12.


<script type = "text/javascript">

var birthdate = prompt ("Enter your age in YYYY/MM/DD format","");

var bdate = new Date(birthdate);
if ( isNaN(bdate) ) {
alert ('Invalid age format');
}
else {
var now = new Date();
var age = now.getFullYear() - bdate.getFullYear();
var mth = now.getMonth();
var bmth = bdate.getMonth();
if (mth - bmth <0 ) {age--}
else if ((mth-bmth== 0) && (now.getDate() - bdate.getDate() < 0)) {age--}
alert('Age is ' + age);

}
</script>

thanks guys! this code is what i wanted.
thank you again

Philip M
02-26-2012, 06:53 PM
I notice that in all the scripts
prompt ("Enter your age in YYYY/MM/DD format","");
should of course read
prompt ("Enter your birthdate in YYYY/MM/DD format","");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum