tags:

views:

206

answers:

2

I have the following function

DateTime fromDateParam = DateTime.ParseExact(Convert.ToString(DateTime.MinValue),"dd.MM.yyyy HH:mm:ss",null);

It says input string not recognised as a valid date.

Any ideas how I can get any the min date recognised to parse exact?

+4  A: 

Well you're converting the original time to a string using the default formatting, but then you're specifying custom formatting for the parsing.

If you specify a format string using DateTime.ToString(format) and keep the format consistent, it works fine:

string formatString = "dd.MM.yyyy HH:mm:ss";
string text = DateTime.MinValue.ToString(formatString);
Console.WriteLine(text);
DateTime fromDateParam = DateTime.ParseExact(text, formatString, null);
Jon Skeet
Thanks Jon, that worked first time round :)
JL
+1  A: 

In other words (continuing Skeet's answer), Convert.ToString(DateTime.MinValue) is based on current/default CultureInfo, etc.

Ron Klein