Monday, January 11, 2010

DateTime.ParseExact bug when converting yymmdd format :-)

I have not seen many applications use yymmdd format but I had to get some date from our legacy server which had yymmdd date on one of the column. I always used DateTime.ParseExact method to parse the date and get the format which I wanted. Here also I had used the same.

Recently, one of the rows had a date like 300101 which is actually 2030-01-01 but this method was converting it to 1930-01-01.

objDateTime = DateTime.ParseExact(“300101”, "yymmdd", System.Threading.Thread.CurrentThread.CurrentCulture);

Strange!!

This seems to be odd and I tried passing 290101 and it converted correctly as 2029-01-01. I am still kind of thinking that this is a bug in this method but I am not sure if this method was intended to do this.It was converting to Century as 19 only when the year has more than 2030. But anyways I have changed my prgram to change the format to yyyymmdd if the year is more than 20 years from now. Just thought it might help if someone using yymmdd.

No comments:

Post a Comment