/    Sign up×
Community /Pin to ProfileBookmark

Calculate a persons age once they input their birthday

Hi there

I’m creating an html form ([I]has to be html cause it uses FP components[/I]) which sportsmen and women will fill in. They need to input their birthday on the form (among other info). On submission they are redirected to a (FP) custom confirmation page where [B]I need their age to appear[/B]. I have no idea how to do this calculation or how to have the result/age appear on the confirmation page. Is this possible with JavaScript? If so, can someone help? I don’t know JavaScript [B][I]at all[/I][/B] ?

Thanks
Mozie

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 11.2005 — This sort of thing is best not done with JavaScript. Have the form submit to a server side script and have that script adjust the returned page as needed.

Be careful, however, when working with dates. Programming languages tend to store dates internally as some number of ticks from so arbitrary starting date, often 1 January 1970. And you'll be tempted to just subtract one date's ticks from the other's and divide by the number of ticks in a year. Do not do this. Because of daylight savings you'll be off sometimes.
Copy linkTweet thisAlerts:
@MozieauthorAug 11.2005 — Thanks Charles, but this is all a bit above me :eek:

What if I go for something really simple like the person inputs the year they are born into the form and the calculation subtracts that year from 2005 (I could change the current year manually) and then their age shows on the confirmation page. Is this as simple as I'm making out it could be? Or not... ?
Copy linkTweet thisAlerts:
@Mr_JAug 11.2005 — Why not just have them input their age?
Copy linkTweet thisAlerts:
@Tweak4Aug 11.2005 — What if I go for something really simple like the person inputs the year they are born into the form and the calculation subtracts that year from 2005 (I could change the current year manually) and then their age shows on the confirmation page. Is this as simple as I'm making out it could be? Or not... [/QUOTE]

Well, it's not that simple at all...

Right now, it is 2005 and I am 27 years old. A month from now, it will still be 2005, but I'll be 28 years old.

Subtracting the birth year from the current year only works if you assume that everyone was born at 11:59:59pm 12/31. Otherwise, you'll need to consider the month and day as well if you want to ensure accuracy.

The logic for doing this in Javascript is relatively easy (as long as you don't consider DST, which would only make a difference in a small percentage of cases, and would vary by location anyway). The problem is that when you retrieve the current date in JS, it retrieves the date on the user's PC, which may be correct, or it may not...

By doing something on the server-side instead, you just have to ensure that the server's date is correct, which is much easier to do.
Copy linkTweet thisAlerts:
@MozieauthorAug 12.2005 — Thanks Mr J & Tweak4... points taken...
×

Success!

Help @Mozie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...