How to calculate age?

I need to read date of birth from excel sheet and calculate the age and write the age to the excel sheet. How that can be done in UiPath? Can somone help me

Hi @sonia_thomas

You can use the DateDiff method available from the DateTime class.

  1. Get the DateDiff between date of birth and today’s date.
  2. Use the DateInterval.Year to get the difference in years.
3 Likes

Hello!

You can use the DateDiff function.

Example:

Syntax: DateDiff(DateInterval, Date1,Date2)
Code: DateDiff(DateInterval.Day, “2016-01-30”,“2016-01-30”)

It will return the number of the days between the two dates as Integer Type.

Hope It helps!

Regards

Hello Sonia,
In this video I do a lot of stuff with DateTime:
In your case, you can do the difference between YEARS after you convert from String to DateTime, or you can use TimeSpan from here(TimeSpan Struct (System) | Microsoft Learn)
UiPath Tutorial for String and DateTime (Use-cases) - YouTube

20:05 DateTime to string in multiple ways
23:00 AddDays get DayofWeek
25:40 Convert from String to DateTime
26:20 Compare DateTime

Thanks,
Cristian Negulescu

Hi,

I guess you want the age in years as most people think that way.

So, if you think it through with common sense, how do you tell someones age?

Current year - birthday year is the starting point, but if he was born in an earlier month than it is now, then we substract 1.

If it’s the same month, then we check the days. If the today’s day is a later date than his birthday’s day, than we substract 1 again from current year - birth year.

You can think from the other direction,
If his birth month passed the current month, then the age is the yearnow-his year.
If the birth month did not pass, then year now-birth year-1,
If the month is same, then if birthday’s day passed today’s day then yearnow-birthyear otherwise yearnow-birthyear-1.

Even I can’t follow it as I am typing it, I made the one liner code, that I can follow better than what I just said here :wink:

An approach without nested if