Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime TryParse - mapping '99' to 2099, not 1999 [duplicate]

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.

like image 537
Chris Avatar asked Aug 26 '11 16:08

Chris


1 Answers

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);
like image 176
p.campbell Avatar answered Sep 27 '22 23:09

p.campbell