简体   繁体   English

DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime

[英]DateTime.ParseExact give the error: String was not recognized as a valid DateTime

I want to parse DateTime, here my code: 我想解析DateTime,这是我的代码:

var datastring =p1.ItemArray[2].ToString();
var format = "dd.MM.yyyy";
var date = DateTime.ParseExact(datastring,format,CultureInfo.InvariantCulture);

p1 - is DataRow , in p1.ItemArray[2] I have value of DateTime p1.ItemArray[2]DataRow ,在p1.ItemArray[2]我有DateTime的值

In watch p1.ItemArray[2] have this value : "09/03/2012 00:00:00" his type is DateTime 在手表p1.ItemArray[2]中,此值是: "09/03/2012 00:00:00"其类型为DateTime

After parsing throws error: String was not recognized as a valid DateTime. 解析后引发错误: 无法将字符串识别为有效的DateTime。

You could include the time in your format as it seems the string you are trying to parse contains the time: 您可以在格式中包含时间,因为您要解析的字符串似乎包含时间:

var format = "dd/MM/yyyy hh:mm:ss";

Also I would recommend you using the TryParseExact method as it provides a better defensive programming pattern instead of throwing exceptions. 我也建议您使用TryParseExact方法,因为它提供了更好的防御性编程模式,而不是引发异常。

IMHO you should use 恕我直言,你应该使用

var datastring = p1.ItemArray[2].ToString();
var format = "dd/MM/yyyy HH:mm:ss";
var date = DateTime.ParseExact(datastring, format, CultureInfo.InvariantCulture);

With ParseExact you must provide exact format of date contained in string 使用ParseExact您必须提供字符串中包含的确切日期格式

Change this: 更改此:

var format = "dd.MM.yyyy";

With this: 有了这个:

var format = "dd/MM/yyyy HH:mm:ss";

because parseExact will expect to receive the same format that you specified which is "dd.MM.yyyy". 因为parseExact将期望接收与您指定的格式相同的“ dd.MM.yyyy”。 You can try: 你可以试试:

var format = "dd.MM.yyyy";
var datastring =p1.ItemArray[2].ToString(format);

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 但是请确保这不会与您的应用程序发生冲突

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

相关问题 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime Datetime.ParseExact“字符串未被识别为有效的日期时间”错误 - Datetime.ParseExact “String was not recognized as a valid DateTime” error DateTime.ParseExact引发以下错误“字符串未被识别为有效的DateTime” - DateTime.ParseExact is raising the following error “String was not recognized as a valid DateTime” 无法将字符串识别为DateTime.ParseExact的有效参数 - String was not recognized as a valid parameter for DateTime.ParseExact 使用DateTime.ParseExact时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime when using DateTime.ParseExact DateTime.ParseExact赋予String未被识别为有效的DateTime。 - DateTime.ParseExact gives String was not recognized as a valid DateTime. DateTime.ParseExact FormatException字符串未被识别为有效的DateTime - DateTime.ParseExact FormatException String was not recognized as a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM