1

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?

JL.
  • 78,954
  • 126
  • 311
  • 459

2 Answers2

4

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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

Ron Klein
  • 9,178
  • 9
  • 55
  • 88