I am having trouble with exercise 5 in chapter 5. You ask to calculate the users age from them entering their birth date.
I have the following code which I am confident should work but when I input my values I keep triggering the validation I have set up
Code:
int age;
DateTime birthday;
bool flag;
//Get the current date
DateTime currentTime = new DateTime();
currentTime = DateTime.Now;
//Check the input
flag = DateTime.TryParse(txtBirthday.Text, out birthday);
if (flag == false)
{
MessageBox.Show("Please use the format, MM/DD/YY", "Input Error");
txtBirthday.Focus();
return;
}
age = currentTime.Year - birthday.Year;
lblAge.Text = age.ToString();
Am I inputting in the values wrong? Or have I done something wrong in the code?
Values I have tried inputting are as follows:
MM/DD/YY
MM/DD/YYYY
M/DD/YYYY
And then the same again without seperators and then using commas as seperators.
Obviously I am replacing the letters with corresponding numeric values but I was pretty confident what I had written would work and I am confused as to why it isn't.
Most likely I am being a numpty and just need another pair of eyes to point it out to me. Any help would be appreciated.