Age Calculation in Javascript

A Javascript function to get the number of years passed since someone's birthday... i.e. Age, is a useful and commonly sought-after addition to a web-designers toolbox.

The following function simply subtracts the difference in years between Now and the birthdate in question, and then adjusts it by a year if the month and day of the birthday fall earlier in the year.

It doesn't validate the date, and it only deals in whole number years meaning babies ages in months are not considered.

To use this function in your web-page, copy the above script snippet and paste it before the </head> tag.

You can then call the function from any point by passing the function a date string in the format dd/mm/yyyy.

In the following example, the onblur event is used on a date entry text box, and the result updates an adjacent div.


Also in PHP!


Return to Javascript Articles