Calculate the age of a person from 2 DateTime is a problem that is often found in programming and also in C #.
Here is a solution that allows you to easily and simply calculate someone’s age in C #.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System; class MainClass { public static void Main (string[] args) { DateTime bday=new DateTime(1990,12,15); DateTime now = DateTime.Today; int age = now.Year - bday.Year; if (bday > now.AddYears(-age)) age--; Console.WriteLine(age); } } |
Here is the C # solution to calculate the age of a person with DateTime.
DateTime represents moments (like the timestamp), they can sometimes seem complicated to handle to do simple things but we learn very quickly to master them and not to do without them.
The details of the C # lines:
C # 1: we initialize a DateTime to retrieve today’s date (today) and not have to rewrite DateTime.Today in the following lines.
C # 2: we calculate an “age” by subtracting the year of the birthday from the current year.
The age you get at this level will not always be good.
Imagine that it is March 10, 2011 and we want to calculate in C # the age of a person born on July 7, 2000
Current year – Anniversary year = 2011 – 2000 = 11
While the person will not be 11 years old until July 7, 2011
The last C # line is there to correct the problem
C # 3: if the birthday is higher (after) the current date minus the age, we withdraw 1 year.
Example: The Datetime of the birthday: July 7, 2000 is actually larger than the current date – age = March 10, 2011 – 11 = March 10, 2000
07/07/2000> 03/10/2000 so we withdraw 1 year, which gives us an age of 10 years.
We can then do a function that calculates the age in C # for us and that takes a DateTime (birthday) as a parameter.
1 2 3 4 5 6 7 8 9 10 | public int CalculAge(DateTime anniversaire) { DateTime now = DateTime.Today; int age = now.Year - anniversaire.Year; if (anniversaire > now.AddYears(-age)) age--; return age; } |
Calculate Age in C# (Years,Months,Days)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | using System; public class Age { public int Years; public int Months; public int Days; public Age(DateTime Bday) { this.Count(Bday); } public Age(DateTime Bday, DateTime Cday) { this.Count(Bday, Cday); } public Age Count(DateTime Bday) { return this.Count(Bday, DateTime.Today); } public Age Count(DateTime Bday, DateTime Cday) { if ((Cday.Year - Bday.Year) > 0 || (((Cday.Year - Bday.Year) == 0) && ((Bday.Month < Cday.Month) || ((Bday.Month == Cday.Month) && (Bday.Day <= Cday.Day))))) { int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month); int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day); if (Cday.Month > Bday.Month) { this.Years = Cday.Year - Bday.Year; this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth); this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth; } else if (Cday.Month == Bday.Month) { if (Cday.Day >= Bday.Day) { this.Years = Cday.Year - Bday.Year; this.Months = 0; this.Days = Cday.Day - Bday.Day; } else { this.Years = (Cday.Year - 1) - Bday.Year; this.Months = 11; this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day); } } else { this.Years = (Cday.Year - 1) - Bday.Year; this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth); this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth; } } else { throw new ArgumentException("Birthday date must be earlier than current date"); } return this; } } |