How to calculate the Age from the DOB

Dear All,

I wanna get the current age based on the DOB, for example if the dob is 31/12/1999 then todays age to be 24 years,

After getting the age I need to convert it into months like 24*12= 288, if the person age is 24 years 10 months then the total should be 288+10=298

Please do suggest me on this

Thanks in advance!

Hi!

You could try this:

months = DateDiff(DateInterval.Month, yourDate, Today())

Hi @naveen.s

Can you try this

dob = DateTime.ParseExact("31/12/1999", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)

totalMonths = ((DateTime.Now.Year - dob.Year) * 12 + DateTime.Now.Month - dob.Month - If(DateTime.Now.Day < dob.Day, 1, 0)).ToString()

Regards,

we recommend to resharpen the needs:

  • years needed -
    OR
  • months count needed - definition of the fractions is needed

We also would highlight that the DateDiff method is not handling fractions and can lead to side effects

for years:
grafik

1 Like

Hi @naveen.s

It can be help to you

image

new DateTime(1999,12,31)
cint(DateDiff(DateInterval.Month, DOB, today))

My requirement is:

1.Fetch the DOB from an web application(for example 12/12/1999)
2. Calculate the age using DOB (From 12/12/1999 to till today)=24years
3. Then convert this years to Months i.e 24 years 10 months=298 months
4. Fetch the tenure from the application for example =58 months
5. Add total months+tenure=298+58=356
6. Then need to convert the 356 months to years i.e 356/12=29.66(29 round off)

done with above:

Steps 3-5,6 are more implementations and not requirements

In general we feel that that the following is needed:

  • get the number of months for a birthday on todays base
  • return it as totalMonth in a rounded fraction way

so we could also also argue:
Dec-1999: ((31-31)+1) / 31 = 0.032258064516129031 Month
Jan-2000 to Okt-2024 = 24 Years * 12 + 9 Months = 297
today: 14.10.2024: 14/31 = 0.45161290322580644

So: we come up to ~ 297,483870 Months
Compared to:
grafik

UPD1 - fixed on calculation issue on years

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.