# Thread: Age Calculator

1. cme
New Comer (less than 5 posts)
Join Date
May 2017
Posts
1
Thanks
0
Thanked 0 Times in 0 Posts

## Age Calculator

Hi All

I am looking for an age calculator to show the age of a person on a web site.

I found one on JavaScript.com

http://www.javascriptkit.com/script/...ulateage.shtml

However The Age Calculator Script is not always accurate.

I am trying to use this script for a web site to tell family members when my kids birthday are and how many days until their birthday

E.g. http://family.elksys.com.au/time/

It works for most of the year. However from 5 days out it fails and make the person older than what they are

E.g.
If I put bill gates birthday as 31st of May 1955 and run the script on today 25th May 2017
displayage(1955, 05, 31, "years", 0, "rounddown")
It works and says that Bill is 61 years old which is correct.

JavaScriptKit.com is 7122 days old!
211 months has passed since the Millennium.
Bill Gates is 61 years old.

However
If I put bill gates birthday as 30th of May 1955 and run the script on today 25th May 2017
displayage(1955, 05, 30, "years", 0, "rounddown")
It works and says that Bill is 62 years old which is not true for another few days.

JavaScriptKit.com is 7122 days old!
211 months has passed since the Millennium.
Bill Gates is 62 years old.

How do I make the script more accurate and not show him older than he is
So it can be accurate for the family web site.

If not does anyone have a script that will accurately show someones age.

Thanks

Craig

2. I haven't really tracked this all the way through the script, however, it's obvious that the initial variable for milliseconds in a year is set incorrectly:

Code:
`var one_year=1000*60*60*24*30*12`
12 times 30 is 360. But there are 365 (technically 365.2422) days in a year (5 days off, hmm). So you can adjust the variable to:

Code:
`var one_year=1000*60*60*24*365`
Or even to (seems to work as well):

Code:
`var one_year=1000*60*60*24*365.2422`
After either of those, at least the error you pointed out disappears, and since it includes the exact number of days in your error, I tend to think it should take care of it. I'll leave it to you to experiment to see whether 365 or 365.2422 is best in all cases, though I imagine some sort of test for leap years might be in order, though would probably only result in a one day issue at certain times of the year during leap years, and that the full number (365.2422) would be most accurate for most cases. During a leap year for birthdays after Feb, you might need to add (or maybe subtract, probably add) one day somewhere in the calculation process, but I'm not sure exactly where or how to determine that, though it's a fairly simple calculation to determine if a given year is a leap year and using the full (265.2422) value should cover previous leap years in most cases, as it would likely accumulate properly into the calculations. It (this one day variance for leap year birthdays after Feb) could not be added/subtracted at the same place we are editing here though, because that would make all years leap years. I'll probably give this some more thought and get back to you if I see an obvious fix for that part of it.

But, as I say, this takes care of the issue in most cases.

The browser cache may have to be cleared and/or the page refreshed to see changes.

3. OK, rather than mess with the existing script to correct for leap years, and since its months calculation was also faulty because not all months have 30 days, I decided to start from scratch and use the date object itself to keep things accurate. Let me know if you have any problems or questions:

Code:
```<!DOCTYPE html>
<html>
<head>
<title>How Old - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script>
// howold Script (c)2017 John Davenport Scheuer
// as first seen in http://www.dynamicdrive.com/forums/
// username: jscheuer1 - This Notice Must Remain for Legal Use
function howold(pastdate, units, round, id){
if(isNaN(new Date(pastdate))){return document.getElementById(id).innerHTML = 'Invalid Date';}
var pd = new Date(pastdate), now = new Date(), cf = round === 'down'? 'floor' : round === 'round'? round : 'ceil',
pdm = (function(){var p = new Date(pastdate); p.setFullYear(now.getFullYear()); return p.getMonth();})(), wu = units.toLowerCase(),
yrs = now.getFullYear() - pd.getFullYear(), mos = now.getMonth() - pdm, samemonth = now.getMonth() === pd.getMonth(), r, ru;
if(round === 'down'){
if(wu === 'months' && now.getDate() < pd.getDate()){--mos;}
if(wu === 'years' && (mos < 0 || (samemonth && now.getDate() < pd.getDate()))){--yrs;}
}
if(wu === 'years'){r = yrs;}
else if(wu === 'months'){r = yrs * 12 + mos;}
else if(wu === 'days'){r = Math[cf]((now.getTime() - pd.getTime()) / (1000 * 60 * 60 * 24));}
else {r = 'Invalid Units'; units = ''};
ru = r === 1? units.replace('s', '') : units;
document.getElementById(id).innerHTML = r + ' ' + ru;
}
</script>
</head>
<body>
<div>April 30 2017 was <span id="a30"></span> Ago.</div>
<div>Bill Gates is <span id="bill"></span> Old.</div>
<div>March 15th, 2017 was <span id="dayexample"></span> ago.</div>
<script>
/* Usage (note - Units are case insensitive. The round parameter may be set to 'round' if the units are days):

howold(pastdate[valid date format], units['years', 'months', 'days'], round['up', 'down'], id['id_of_element' to write to]);

Typical Outputs (units returned as entered, made singular if the number is one):
1 Month
61 Years
72 days
*/
howold('April, 30, 2017', 'Months', 'up', 'a30');
howold('Oct, 28, 1955', 'Years', 'down', 'bill');
howold('Mar, 15, 2017', 'days', 'round', 'dayexample');
</script>
</body>
</html>```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•