Just how do I compute a person's age in C#?

Offered a DateTime standing for an individual's birthday celebration, just how do I compute their age in years?

2019-05-06 23:17:14
Source Share
Answers: 6

I do not assume any one of the solutions until now attend to societies that compute age in different ways. See, as an example, East Asian Age Reckoning versus that in the West.

Any kind of actual solution needs to include localization. The Strategy Pattern would possibly remain in order in this instance.

2019-12-03 04:31:03

This is a weird means to do it, yet if you layout the day to yyyymmdd and also subtract the day of birth from the existing day after that go down the last 4 figures you've obtained the age

I do not recognize C#, yet I think this will certainly operate in any kind of language.

20080814 - 19800703 = 280111 

Drop the last 4 figures = 28.

C# Code:

int now = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int dob = int.Parse(dateOfBirth.ToString("yyyyMMdd"));
int age = (now - dob) / 10000;

Or conversely without all the type conversion in the kind of an expansion method. Mistake monitoring left out:

public static Int32 GetAge(this DateTime dateOfBirth)
    var today = DateTime.Today;

    var a = (today.Year * 100 + today.Month) * 100 + today.Day;
    var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;

    return (a - b) / 10000;
2019-05-31 19:07:26

This is the variation we make use of below. It functions, and also it's rather straightforward. It's the very same suggestion as Jeff's yet I assume it's a little more clear due to the fact that it divides out the reasoning for deducting one, so it's a little less complicated to recognize.

public static int GetAge(this DateTime dateOfBirth, DateTime dateAsAt)
    return dateAsAt.Year - dateOfBirth.Year - (dateOfBirth.DayOfYear < dateAsAt.DayOfYear ? 0 : 1);

You can expand the ternary driver to make it also more clear, if you assume that type of point is vague.

Clearly this is done as an expansion method on DateTime, yet plainly you can order that line of code that does the job and also placed it anywhere. Below we have an additional overload of the Extension method that comes on DateTime.Now, simply for efficiency.

2019-05-10 06:02:05

The ideal manner in which I recognize of as a result of jump years and also every little thing is :

DateTime birthDate = new DateTime(2000,3,1);
int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);

Hope this aids.

2019-05-08 20:40:55

An understandable and also straightforward remedy.

// Save today's date.
var today = DateTime.Today;
// Calculate the age.
var age = today.Year - birthdate.Year;
// Go back to the year the person was born in case of a leap year
if (birthdate > today.AddYears(-age)) age--;

However, this thinks you are seeking the western suggestion old and also not making use of East Asian reckoning.

2019-05-08 17:46:59

Another function, not by me yet located on the internet and also improved it a little bit :

public static int GetAge(DateTime birthDate)
    DateTime n = DateTime.Now; // To avoid a race condition around midnight
    int age = n.Year - birthDate.Year;

    if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))

    return age;

Simply 2 points that enter into my mind : What concerning individuals from nations that do not make use of the gregorian schedule? DateTime.Now remains in the server-specific society i assume. I have definitely 0 expertise concerning in fact collaborating with Asian schedules and also I do not recognize if there is a very easy means to transform days in between schedules, yet simply in instance you're questioning those chinese individuals from the year 4660 :- )

2019-05-07 16:51:14