C# DateTime.ParseExact 给出 String 未被识别为有效的 DateTime。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11766022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:09:01  来源:igfitidea点击:

DateTime.ParseExact gives String was not recognized as a valid DateTime.

c#asp.netparsingdatetime

提问by DesignMonkeyDK

I'm trying to parse a date string into a DateTimevariable. I've found out that ParseExactis the way to do it, but I try this I get the error:

我正在尝试将日期字符串解析为DateTime变量。我发现这ParseExact是这样做的方法,但我尝试这样做时出现错误:

String was not recognized as a valid DateTime.

字符串未被识别为有效的 DateTime。

string timeFormat = "dd-MM-yyyy hh:mm:ss";
DateTime startDate = DateTime.ParseExact(reader["startdate"].ToString(), timeFormat, CultureInfo.InvariantCulture);
DateTime nextDate = DateTime.ParseExact(reader["nextdate"].ToString(), timeFormat, null);

I've tried both with null(which happens to work on another page), and the CultureInfo.InvariantCulture.

我已经尝试过null(碰巧在另一个页面上工作)和CultureInfo.InvariantCulture.

reader["startdate"].ToString()output: 01-08-2012 15:39:09

reader["startdate"].ToString()输出:01-08-2012 15:39:09

and

reader["nextdate"].ToString()output: 01-08-2012 15:39:09

reader["nextdate"].ToString()输出:01-08-2012 15:39:09

I think it should work, but it doesn't.

我认为它应该工作,但它没有。

Somebody have an idea what is wrong? :)

有人知道有什么问题吗?:)

采纳答案by Jon Skeet

You're using hhin your format string. That's a 12-hour "hour of day" field. The value 15 isn't in range...

hh在格式字符串中使用。这是一个 12 小时的“一天中的小时”字段。值 15 不在范围内...

You want HHinstead, which is the 24-hour specifier.

您想要的HH是,这是 24 小时说明符。

See the MSDN custom date and time format strings documentationfor more information.

有关详细信息,请参阅MSDN 自定义日期和时间格式字符串文档

回答by Hassan Boutougha

try this it works

试试这个它有效

DateTime.ParseExact("01-08-2012 15:36:25", "dd-MM-yyyy HH:mm:ss", null);

回答by Rochelle Cassar

I am not sure if this helps but I used the exact code from this article and it worked for me because the DateTime.ParseExact(dat, "dd/MM/yyy HH:MM", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None) did not work for me.

我不确定这是否有帮助,但我使用了本文中的确切代码并且它对我有用,因为 DateTime.ParseExact(dat, "dd/MM/yyy HH:MM", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.无)对我不起作用。

Read this I just posted it online: http://rochcass.wordpress.com/2012/08/27/error-string-was-not-recognized-as-a-valid-datetime-solution/#more-350

阅读我刚刚在网上发布的内容:http: //rochcass.wordpress.com/2012/08/27/error-string-was-not-recognized-as-a-valid-datetime-solution/#more-350

回答by Anoop Aravind

Most probably due to the difference between your Server locale and the UI locale

很可能是由于您的服务器区域设置和 UI 区域设置之间的差异

One easier method will be to specify the globalization details in the web.config

一种更简单的方法是在 web.config 中指定全球化详细信息

like

喜欢

<configuration>
   <system.web>
      <globalization culture="en-GB"/>
   </system.web>
</configuration>

OR in more detail

或更详细

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" />

But be sure this wont clash with your application in general

但请确保这不会与您的应用程序发生冲突