I have a problem while converting a string whose value is dd.mm.yyyy to DateTime in c#
string OriginalDateFormat = "28.06.2009";
DateTime dt= Convert.ToDateTime(OriginalDateFormat);
Throwing an exception "String was not recognized as a valid DateTime."
But if it is in mm.dd.yyyy then it is running fine.
I googled and got lots of sites but all in vain
Any idea?
Thanks in advance.
Use
DateTime.ParseExactand specify the exact format string:If this is the value is from user input, you probably want to use DateTime.TryParseExact so you can handle failure gracefully: