问题
I have the following string that I need to parse
string date = "2017-06-23T13:45:45.816"
What is correct format string?
I tried
DateTime createDate = DateTime.ParseExact(date, "yyyy-MM-dd'T'hh-mm-ss", CultureInfo.InvariantCulture);
回答1:
yyyy-MM-dd'T'hh-mm-ss
is not 2017-06-23T13:45:45.816
you have missing milliseconds, 12 hour clock and also wrong separators.
You'd probably need something like:
"yyyy-MM-dd'T'HH:mm:ss.fff"
Remember it's ParseExact.
回答2:
That seems an RFC 3339 date to me, so Convert.ToDateTime
or DateTime.Parse
will do.
string date = "2017-06-23T13:45:45.816";
DateTime dt = Convert.ToDateTime(date);
回答3:
Firstly You should Replace That 'T' With Blank Space ' '
string date = "2017-06-23T13:45:45.816";
date = date.Replace("T"," "); // you can use this code to replace
DateTime myDate = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(myDate.ToString("yyyy-MM-dd HH:mm:ss.fff"));
// you can try this code might be it works for u
来源:https://stackoverflow.com/questions/44885467/how-to-parse-datetime-with-datetime-parseexact