如何以这种格式解析时间2019-07-30 16:45:00.9 c#



我以这种格式接收日期字符串:

2019-07-30 16:45:00.9

2019-08-01 09:00:00.0

2019-08-01 11:30:00.0

我使用的代码在下午12点之前正常工作。之后,它无法正确解析

string dateString, format;  
      DateTime result;
      CultureInfo provider = CultureInfo.InvariantCulture;

dateString= "2019-07-30 16:45:00.9";
format = "yyyy-MM-dd hh:mm:ss.f";
try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());  }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

您的当前格式为12小时时钟,这就是为什么12 pm之后的任何事情都无法解析。

使用Capital HH以您的格式使用24小时。

format = "yyyy-MM-dd HH:mm:ss.f";

相关内容

最新更新