Last active
June 8, 2023 11:40
-
-
Save faisalman/1724253 to your computer and use it in GitHub Desktop.
Calculate Age (Years + Months + Days) in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculate Age in C# | |
* https://gist.github.com/faisalman | |
* | |
* Copyright 2012-2013, Faisalman <[email protected]> | |
* Licensed under The MIT License | |
* http://www.opensource.org/licenses/mit-license | |
*/ | |
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; | |
} | |
} | |
/** | |
* Usage example: | |
* ============== | |
* DateTime bday = new DateTime(1987, 11, 27); | |
* DateTime cday = DateTime.Today; | |
* Age age = new Age(bday, cday); | |
* Console.WriteLine("It's been {0} years, {1} months, and {2} days since your birthday", age.Year, age.Month, age.Day); | |
*/ |
I use this code to calculate Age and it is very simple:
`DateTime birthdays = DateTime.Parse(dateTimePicker1.Text);
DateTime dateNow = DateTime.Parse(dateTimePicker2.Text);
TimeSpan urBirthday = dateNow - birthdays;
label3.Text = urBirthday.Days.ToString();
label3.Text += "\n All Day :" + string.Format("{0} total days ",urBirthday.TotalDays);
int nowMonth = dateNow.Month;
int birthMonth = birthdays.Month;
int nowYears = dateNow.Year;
int birYear = birthdays.Year;
int nowDays = dateNow.Day;
int biDays = birthdays.Day;
int yy = nowYears - birYear;
int mm = nowMonth - birthMonth;
int dd = nowDays - biDays;
if (mm < 0)
{
yy--;
mm = mm + 12;
}
if (dd < 0)
{
mm--;
dd = dd + 31;
}
label3.Text += "\n\n year :" + string.Format("{0} years",yy);
label3.Text += "\n month :" + string.Format("{0} months",mm);
label3.Text += "\n days :" + string.Format("{0} days",dd);`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Im learning can anyone please explain the code ?