I have a textbox where users can enter a year “YY” and I am parsing it using:
int year;
if (int.TryParse(txtYear.Text, out year))
{
args.Year = year;
}
The problem is TryParse will convert ’07’ into ‘7’ and my method is expecting the year to be in the format YYYY (e.g. input “07” should be “2007”). What is the best way to do this conversion?
The best way is to request the user to enter all four digits. Part of the reason for the 2K problem was legacy systems that assumed 2 digits would always be enough.
If you have to do this, then just add 2000 to your result.
You might want to add 1900 instead of 2000 for for sufficiently large inputs (“99” -> 1999, “00” -> 2000) depending on how likely it is that your users intended 2099 when they entered “99”.