# Calculating an age from a birth date

Help needed!!

How can I use a date of birth to show as a persons age?

CurrentYear - BirthYear = Age.

Ok, less snarky: Age to the day? To the month? To the year? The minute? Whatâ€™s the input for their birthday look like? Are they inputting Day/Month/Year? Just Year? Lunar months since birth?

Hahaha! Thank you! I was way over thinking it.

You can get a little more advanced if you like. If you use something like this:

``````[[Math.floor((Now.valueOf() - Date.parse('02/28/1989').valueOf()) / 1000 / 60 / 60 / 24 / 365)]]
``````

For example this will give 28 years before Feb 28 and 29 years after Feb 28, if you want to account for whether weâ€™ve passed their birth date or not in the year. Itâ€™s not perfect as it doesnâ€™t account for leap years but itâ€™s probably good enough.

(Note the American mm/dd/yyyy date format in the .parse method)

1 Like

Hello there.

Letâ€™s say the respective date has to be entered in a text input formfield or be picked from the variables. How do I point this function to parse that date instead of the predefined, static 02/28/1989 ?

â€¦ already got it. Had a bug that confused me

For anyone else wondering, you would replace the static date in the expression with a variable that you would then set when someone typed in a date into a text field.

sorry to be such a dweeb but does anyone have an example file of this age calculator (in v8)? I want to calculate an age from current date to date entered. User enters a date of birth, then calculation only returns age in years, no need for days, monthsâ€¦yet.

I can get it to work using the fx string above but would prefer to have it calculate on any date entered rather than a fixed date.

And Iâ€™ve tried both global and local variables without success. Iâ€™m definitely missing something

Update: Iâ€™ve got this string to work except Iâ€™m getting decimal places and trying not to.

[[(Now.valueOf() - Date.parse (LVLage).valueOf()) / 1000 / 60 / 60 / 24 / 365 .toFixed(0) ]]

Guessing Iâ€™m placing â€ś.toFixed()â€ť in the wrong spot or my syntax is wrong??

Try this:

``````[[((Now.valueOf() - Date.parse(LVLage).valueOf()) / 1000 / 60 / 60 / 24 / 365).toFixed(0)]]
``````

The `.toFixed()` method needs to be called on something, so by wrapping the entire expression in parens `()` you can call `.toFixed()` on what it evaluates to.

Hereâ€™s it shown more clearly:

``````[[
(
(Now.valueOf() - Date.parse(LVLage).valueOf()) / 1000 / 60 / 60 / 24 / 365
).toFixed(0)
]]
``````

Thank you @nkrisc, that did the trick. I appreciate the help.
Happy holidays