Age in years from DateTime (Date of birth)

Duplicate

How to calculate the age of a certain person in C #?

I have a datetime variable that represents the user's date of birth.

How can I get age in years from this?

Update I want an exact birthday, so 30.45 years or something like that.

+6
c # datetime
source share
3 answers

Try the following (assuming the date of birth is stored in dtDOB ):

 public int getAgeInYears { TimeSpan tsAge = DateTime.Now.Subtract(dtDOB); return new DateTime(tsAge.Ticks).Year - 1; } 
+5
source share

Stolen from the answer to Jeff's question:

 DateTime now = DateTime.Now; int age = now.Year - bday.Year; if (now < bday.AddYears(age)) age--; 
+4
source share

You can try (in Vb):

  Dim dateOfBirth As Date Now.Subtract(dateOfBirth).TotalDays \ 365 

\ is a whole division in Vb, I don't know if it has in C #.

+1
source share

All Articles