Getting error like :
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: String was not recognized as a valid DateTime.
I am using this code :
string datetime = DateTime.Parse(encrypt[1]);
or
string datetime = Convert.ToDatetime(encrypt[1]);
encrypt is a string array
In encrypt[1]
I am not sure which format will come in string
. i have trace some time come dd/MM/yyyy
and sometime MM/dd/yyyy
or MM-dd-yyyy
or dd-MM-yyyy
. I am not sure about format it may from above or another format come.
also use ParseExcept
and TryParseExcept
. but not get succeeded seems return same error
please give me proper solution.
If you don't exactly know what is coming in, you have virtually no change of getting the date format in correctly.
Take this sample:
Is this the 2nd of January or the 1st of February?
If you do know the formats, you could use
TryParseExact
and just walk down the list until one matches: