How to parse datetime with DateTime.ParseExact

假如想象 提交于 2021-02-04 21:36:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!