Possible Duplicate:
DateTime.TryParse century control C#
I need to TryParse a string such as the following: "01/01/99" into a DateTime object, and I'm using something like this:
DateTime outDate;
if (DateTime.TryParseExact("01/01/99", "dd/MM/yy", null, System.Globalization.DateTimeStyles.None, out outDate))
{
//Do stuff here
}
Of course, this date gets parsed as 01/01/1999, which is not what I want - I want it to parse as 2099. Is there an easy way to do this? Sadly modifying the data I'm parsing to include the full year is not an option.
Taken from this answer, you can supply ParseExact()
with a culture object. Suspect TryParseExact()
would be the same:
CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.LCID);
ci.Calendar.TwoDigitYearMax = 2099;
//Parse the date using our custom culture.
DateTime dt = DateTime.ParseExact(input, "MMM-yy", ci);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With