public void calculateAge(System.DateTime birthDate)
{
Int16 mySpan = Convert.ToInt32(System.DateTime.Now.Subtract(birthDate).TotalDays);
Int16 nowYear = System.DateTime.Now.Year;
Int16 nowMonth = System.DateTime.Now.Month;
Int16 birthYear = birthDate.Year;
Int16 birthMonth = birthDate.Month;
Int16 yearCount = default(Int16);
for (yearCount = System.DateTime.Now.Year; yearCount >= birthYear; yearCount += -1) {
if (yearCount % 4 == 0) {
switch (true) {
case yearCount == nowYear & nowMonth < 3:
break;
//This is a leap year but we're not past Feb yet
//Do nothing
case yearCount == birthYear & birthMonth > 2:
break;
//They were born after Feb in a leap year
//Do nothing
default:
//This was a full leap year, subtract a day to make it 365
mySpan -= 1;
break;
}
}
}
Int16 myYears = mySpan / 365;
Int16 myDays = mySpan - (myYears * 365);
if (myDays < 0) {
myYears -= 1;
myDays = 365 + myDays;
// myDays is negative so this will subtract
}
MessageBox.Show("You are " + myYears + " years and " + myDays + " days old.");
}
DateTime dob = default(DateTime);
dob = new DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day);
TimeSpan tday = DateTime.Now.Subtract(dob);
int years = 0;
int months = 0;
int days = 0;
months = 12 * (DateTime.Now.Year - dob.Year) + (DateTime.Now.Month - dob.Month);
if (DateTime.Now.Day < dob.Day) {
months -= 1;
days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + DateTime.Now.Day;
} else {
days = DateTime.Now.Day - dob.Day;
}
years = Math.Floor(months / 12);
months -= years * 12;
Interaction.MsgBox("Your age as on " + Strings.Format(Now, "dd-MMM-yyyy") + Constants.vbCrLf + years + " Years, " + months + " Months and " + days + " Days");