问题:String was not recognized as a valid DateTime

时间:2021-11-19 10:40:37
Console.WriteLine( " Date is {0} " ,DateTime.Parse( " 11/3/2005 10:29:41 AM " ));
Console.WriteLine(
" Date is {0} " ,DateTime.Parse( " 27/03/2005 10:46:02 AM " ));

        

 

输出的结果:

Date is 2005-11-3 10:29:41
String was not recognized as a valid DateTime

系统认为27为月份了,所以转换失败。

解决:

Console.WriteLine( " Date is {0} " ,DateTime.ParseExact( " 27/03/2005 10:46:02 AM " , " dd/MM/yyyy HH:mm:ss tt " ,System.Globalization.CultureInfo.InvariantCulture));

输出:

2005-3-27 10:46:02