C # calculates exact age

Does anyone know how to get age by date (date of birth)

im thinking about something like this

string age = DateTime.Now.GetAccurateAge(); 

and the output will be about the same as 20Years 5Months 20Days

+6
c #
source share
9 answers
 public static class DateTimeExtensions { public static string ToAgeString(this DateTime dob) { DateTime today = DateTime.Today; int months = today.Month - dob.Month; int years = today.Year - dob.Year; if (today.Day < dob.Day) { months--; } if (months < 0) { years--; months += 12; } int days = (today - dob.AddMonths((years * 12) + months)).Days; return string.Format("{0} year{1}, {2} month{3} and {4} day{5}", years, (years == 1) ? "" : "s", months, (months == 1) ? "" : "s", days, (days == 1) ? "" : "s"); } } 
+36
source share

Check out the answers to How to Calculate People Age in C # for ideas.

+4
source share
+2
source share

I'm not sure that it will always be correct (I did not think about whether there were any cases with leap years, etc., which could lead to its failure ...), but this is an easy way to get out of the year and month: / p>

 DateTime bd = DateTime.Parse("2009-06-17"); TimeSpan ts = DateTime.Now.Subtract(bd); DateTime age = DateTime.MinValue + ts; string s = string.Format("{0} Years {1} months {2} days", age.Year -1 , age.Month - 1, age.Day - 1); 
+2
source share

It sounds like a great exercise to get to know the TimeSpan class better.

+1
source share

Since I cannot post the code in a comment, here is a code based on @LukeH's answer with a bug fixed

 public static int GetAge( DateTime dob, DateTime today, out int days, out int months ) { DateTime dt = today; if( dt.Day < dob.Day ) { dt = dt.AddMonths( -1 ); } months = dt.Month - dob.Month; if( months < 0 ) { dt = dt.AddYears( -1 ); months += 12; } int years = dt.Year - dob.Year; var offs = dob.AddMonths( years * 12 + months ); days = (int)( ( today.Ticks - offs.Ticks ) / TimeSpan.TicksPerDay ); return years; } 
+1
source share

To get the age, I will use the date and date of birth and find the difference between this and the current system time. This link shows how to find the difference between two dates. Just make the start date of the user's birth and end time (DateTime.Now;)

0
source share

Here is what I use:

  public static int GetAge(DateTime dateOfBirth) { int age = DateTime.Now.Year - dateOfBirth.Year; if (dateOfBirth.AddYears(age) > DateTime.Now) { age = age - 1; } return age; } 
0
source share

Try this one

  public static class DateTimeExtension { /// <summary> /// Returns day, month, and year by subtracting date2 from date1 /// </summary> /// <param name="date1">the date from which date2 will be subtracted</param> /// <param name="date2">this date will be subtracted from date1</param> /// <returns></returns> public static string GetAge(this DateTime date1, DateTime date2) { int y = 0, m = 0, d = 0; for (DateTime i = date2; i < date1; i = i.AddDays(1)) { d++; if (d >= DateTime.DaysInMonth(i.Year, i.Month)) { d = 0; m++; } if (m >= 12) { y++; m = 0; } } return "Time-span: " + y + " Year(s), " + m + " Month(s), " + d + " Day(s)"; } } 

Use the method:

  Console.WriteLine(DateTime.Now.GetAge(new DateTime(1990, 1, 3))); 
0
source share

All Articles