Friday, November 21, 2008

Show user exact age in JavaScript (like 28.34)

If you are working on Human Resources project and needs this info or you just want to be more accurate when displaying age.

Here is the code:

function DisplayAge()
{
// that's the user's birthday
month = 11;
date = 18;
year = 1980;

today = new Date();

dateStr = today.getDate();
monthStr = today.getMonth()+1;
yearStr = today.getFullYear();
theYear = yearStr - year;
theMonth = monthStr - month;
theDate = dateStr - date;

var age = theYear + theMonth/12 + theDate/365;


document.write(age.toLocaleString());
}

No comments: