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. :slight_smile:

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 ?

Thanks for your advice…

… already got it. Had a bug that confused me :confused:

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.
Thanks in advance.

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??
Thanks in advance

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

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