Skip to main content
Solved

Calculate an age with a birth day

  • June 9, 2020
  • 4 replies
  • 75 views

philippeb
Enthusiast
Forum|alt.badge.img+22

Hi,

I have birth days in this format YYYY-MM-DD.

I want to calculate their ages.

Per exemple, someone born 1950-03-28 would be 70 years old.

Not sure how to do that. Any idea?

Thanks

Best answer by ebygomm

I'd probably use the Date/Time Functions, so parse the attribute named birthdate into date format, calculate the difference in years from today's date and round down

@floor(@DateTimeDiff(@DateTimeNow(),@DateTimeParse(@Value(birthdate),%Y-%m-%d),Years))

0684Q00000ArKSRQA3.png

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

david_r
Celebrity
  • June 9, 2020

Convert both dates to seconds since epoch (%Es), find the difference, then convert back to years. If you don't need to take leap years into account, the easiest is to divide by 31556952 (the number of seconds in a non-leap year).


david_r
Celebrity
  • June 9, 2020

Or to be completely accurate (using leap years and all), first convert the dates to FME timestamps then use the DateTimeCalculator:


ebygomm
Influencer
Forum|alt.badge.img+46
  • Influencer
  • Best Answer
  • June 9, 2020

I'd probably use the Date/Time Functions, so parse the attribute named birthdate into date format, calculate the difference in years from today's date and round down

@floor(@DateTimeDiff(@DateTimeNow(),@DateTimeParse(@Value(birthdate),%Y-%m-%d),Years))

0684Q00000ArKSRQA3.png


philippeb
Enthusiast
Forum|alt.badge.img+22
  • Author
  • Enthusiast
  • June 9, 2020

Or to be completely accurate (using leap years and all), first convert the dates to FME timestamps then use the DateTimeCalculator:

Nice I didn't know we could do that :D

I need to put an AttributeRounder after to take out de decimals though.

Thanks!!