I have string that displays date and time in the following format:
Thu Jan 03 15:04:29 2013
How would I convert this to a DateTime? I have tried:
string strDateStarted = "Thu Jan 03 15:04:29 2013"
DateTime datDateStarted = Convert.ToDateTime(strDateStarted);
But this does not work. In my program this value is read in from a log file so I am not able to change the format of the text string.
Use one of the *Parse*
methods defined on DateTime
.
Either TryParseExact
or ParseExact
which will take a format string corresponding to the date string.
I suggest reading up on Custom Date and Time Format Strings.
In this case, the corresponding format string would be:
"ddd MMM dd HH:mm:ss yyyy"
To be used:
DateTime.ParseExact("Thu Jan 03 15:04:29 2013",
"ddd MMM dd HH:mm:ss yyyy",
CultureInfo.InvariantCulture)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With