DateTime ParseExact string was not recognize as a DateTime C#

早过忘川 提交于 2019-12-02 13:28:11

You are trying to parse the DateTime exactly, therefore the format has to be exact

DateTime.ParseExact("01/01/0001 12:00:00 AM", "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

You're using ParseExact, meaning you're specifying the whole format of the date and time - and the format specified is "dd/MM/yyyy", which covers "01/01/0001" but what does the poor function do with " 12:00:00 AM"? Look at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx.

Also, it's almost always better to use the "Try" functions for parsing - in this case "TryParseExact"

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