Hi, this an age calculator:
private void button1_Click(object sender, EventArgs e)
{
try
{
if (dtpcurrentdate.Value < dtpdateofbirth.Value)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\Windows\Media\Windows Foreground.wav");
player.Play();
MessageBox.Show("Error Calculating! The Date of Birth is greater than the Current Date. Please Try Again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
lblyears.Text = "";
lblmonths.Text = "";
}
else if (dtpcurrentdate.Value < dtpdateofbirth.Value)
{
lblyears.Text = "";
lblmonths.Text = "";
}
int Age = dtpcurrentdate.Value.Year - dtpdateofbirth.Value.Year;
//if add Dateofbirth+Age <Current Age
Age--;
int AgeinMonths = dtpcurrentdate.Value.Month - dtpdateofbirth.Value.Month;
int ageindays = dtpcurrentdate.Value.Day - dtpdateofbirth.Value.Day;
if (dtpcurrentdate.Value > dtpdateofbirth.Value)
{
lblyears.Text = Age.ToString() + " Year(s) " + AgeinMonths.ToString() + " Month(s)" + ageindays.ToString() + " Day(s)";
}
}
But the problem is it shows some values in negative. For example if select Date of birth date time picker as 7th October 2008 and then select today's date (17th july 2021) in the current date dtp, then it shows the result as: 12 years, -3 months, 10 days. I want it to show 9 months instead of -3 months.
