CA50
Skilled
Hi friends, currently i am developing a .NET app which requires the age of a person.
It will be like this :
- user enters the date-of-birth
- the age is automatically calculated till today.
Age format must be xx-years, xx -months, xx-days
Till now i have developed this algo, but if the age is less then 1 year (365 days), then bugs pops up
Here are the codes in C#
It will be like this :
- user enters the date-of-birth
- the age is automatically calculated till today.
Age format must be xx-years, xx -months, xx-days
Till now i have developed this algo, but if the age is less then 1 year (365 days), then bugs pops up
Here are the codes in C#
Code:
try
{
DateTime todays_date, date_of_birth;
todays_date = DateTime.Today;
//Date of Birth is accepted via user input at the date_time_picker
date_of_birth = dtpTaxCalcForOthers_DOB.Value;
TimeSpan date_difference;
date_difference = todays_date.Subtract(date_of_birth);
//AGE_ALGORITHM
int total_days = date_difference.Days;
int calculated_ageYY, calculated_ageMM, calculated_ageDD, remaining_days;
if ((total_days % 365) == 0)
{
calculated_ageYY = total_days / 365;
calculated_ageMM = 0;
calculated_ageDD = 0;
}
else
{
calculated_ageYY = total_days / 365;
remaining_days = total_days % 365;
if ((remaining_days % 30) == 0)
{
calculated_ageMM = remaining_days / 30;
calculated_ageDD = 0;
}
else
{
calculated_ageMM = remaining_days / 30;
calculated_ageDD = remaining_days % 30;
}
}
//Displaying the result to the text-box
txtTaxCalcForOthers_AgeYY.Text = calculated_ageYY.ToString();
txtTaxCalcForOthers_AgeMM.Text = calculated_ageMM.ToString();
txtTaxCalcForOthers_AgeDD.Text = calculated_ageDD.ToString();
}
catch
{
}