Shows how to get a person's age in years.
Below are two dates, current
today's date, birthDate
date of a person's birth.
int current = 20240403;
int birthDate = 19000924;
Format for both yyyyMMdd
Subtract current from birthDate, remove the last four characters to get years old.
int yearsOld = int.Parse((current - birthDate).ToString()[..^4]);
Variations
1️⃣
Console.WriteLine(
(current - birthDate).ToString()
.RemoveLastCharacters()
.Age());
2️⃣
int now = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int dob = int.Parse(new DateOnly(1900,9,24).ToString("yyyyMMdd"));
int age = (now - dob) / 10000;
Console.WriteLine(age);
3️⃣
Console.WriteLine(new DateOnly(1900, 9, 24).GetAge());