简体   繁体   English

System.FormatException:字符串未被识别为有效的DateTime(DateTime格式yyyyMMdd:HHmmss)

[英]System.FormatException: String was not recognized as a valid DateTime (DateTime Format yyyyMMdd:HHmmss)

I have a date time format of yyyyMMdd:HHmmss . 我的日期时间格式为yyyyMMdd:HHmmss I receive the datetime from a device and I can confirm that the format is yyyyMMdd:HHmmss . 我从设备收到日期时间,并且可以确认格式为yyyyMMdd:HHmmss

For example I get datetime as 20180331:162308 . 例如,我将日期时间设置为20180331:162308 I tried to convert to 03/31/2018 04:23 PM format by using below code: 我试图通过使用以下代码转换为03/31/2018 04:23 PM格式:

string localDateTime = 
  Convert.ToString(protocol.GetParameter(Parameter.localmdmdatetimestr_3043));

string formatString = "yyyyMMdd:HHmmss";

protocol.Log(
 "QA" + protocol.QActionID + "|DateTime|localDateTime" + localDateTime,
  LogType.Error, 
  LogLevel.NoLogging);

DateTime LocalDt = DateTime.ParseExact(
  localDateTime, 
  formatString, 
  CultureInfo.InvariantCulture);

But instead get 但是反而得到

System.FormatException: String was not recognized as a valid DateTime error. System.FormatException:字符串未被识别为有效的DateTime错误。

As per your requirement 根据您的要求
string localDateTime = Convert.ToString("20180331:162308"); 字符串localDateTime = Convert.ToString(“ 20180331:162308”);

        string formatString = "yyyyMMdd:HHmmss";

        DateTime LocalDt = DateTime.ParseExact(
          localDateTime,
          formatString, CultureInfo.InvariantCulture);
        Console.WriteLine(LocalDt);
        var test = LocalDt.ToString("yyyy/MM/dd HH:mm tt", CultureInfo.InvariantCulture);
       Console.WriteLine(test);
        Console.ReadLine();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime c# 中的 DateTime 解析:获取“System.FormatException:”String 未被识别为有效的 DateTime”错误 - DateTime parsing in c#:getting a 'System.FormatException: 'String was not recognized as a valid DateTime' error 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# System.FormatException:字符串未被识别为有效的日期时间 - 尝试转换 MM/DD/YYYY 时 - System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY 如何解决此错误:System.FormatException:'字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.' 试图减去 DateTime “System.FormatException: String '12/9/2019 12:00:00 AM' 未被识别为有效的 DateTime。” - Trying to subtract DateTime "System.FormatException: String '12/9/2019 12:00:00 AM' was not recognized as a valid DateTime." System.FormatException:字符串未被识别为有效的布尔值 - System.FormatException: String was not recognized as a valid Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM