简体   繁体   English

“字符串未被识别为有效的DateTime。”Window 7计算机环境中出现错误

[英]“String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment

Good day All, 美好的一天,

Before this, I have ac# system in a VM with Microsoft Window XP. 在此之前,我在使用Microsoft Window XP的VM中使用了ac#系统。 I have some code to convert string to date time, the following is part of my code : 我有一些代码将字符串转换为日期时间,以下是我的代码的一部分:

DateTime allowDateTime = DateTime.Now.AddMonths(-2);
string formatted = allowDateTime.ToString("M/dd/yyyy");
DateTime dt = Convert.ToDateTime(formatted);

if (redempDateConvert < dt)
   td.Text = "";

Until this point, everything is working fine. 在此之前,一切都运转良好。 After that, I move my all source code without any changes, and data base and set it up in my real machine (Window 7). 之后,我移动我的所有源代码,没有任何更改,数据库并在我的真实机器中设置它(Window 7)。

System is working fine, I am still able to log in and control the system like usual. 系统工作正常,我仍然可以像往常一样登录并控制系统。

Until today, I have reach to this part, and browser displayed error message : String was not recognized as a valid DateTime. 直到今天,我已到达此部分,并且浏览器显示错误消息: String was not recognized as a valid DateTime. in line 397. 在第397行。

Here I displayed my code again (with explanation): 在这里,我再次显示了我的代码(附带说明):

    DateTime allowDateTime = DateTime.Now.AddMonths(-2);
    string formatted = allowDateTime.ToString("M/dd/yyyy");
    DateTime dt = Convert.ToDateTime(formatted);  //here is line 397, which is the error happening.

    if (redempDateConvert < dt)
       td.Text = "";

I have checked both (VM and my real machine) environment, both running in .Net 4.0. 我检查了两个(VM和我的真实机器)环境,两者都在.Net 4.0中运行。

Just curious on why the same code, but there is an error happen in my real machine. 只是好奇为什么相同的代码,但在我的真机中发生了错误。 Is that I miss out to configure something? 我错过了配置的东西吗? Kindly advise. 好心提醒。

It's because / means default date separator, your machines have different cultures. 这是因为/意味着默认日期分隔符,您的机器具有不同的文化。 If you are always getting / as date separator and have culture that accepts - as date separator it will fail. 如果您总是获得/作为日期分隔符并且具有接受的文化-作为日期分隔符,它将失败。

use ParseExact to avoid errors with different culture: 使用ParseExact来避免不同文化的错误:

DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);

code above will parse date with / as date separator no matter which culture you will be using 上面的代码将使用/作为日期分隔符解析日期,无论您将使用哪种文化

使用DateTime.ParseExact()

DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);

Here how Convert.ToDateTime method looks like when you decompile it; 这里Convert.ToDateTime方法在反编译时的样子如何;

public static DateTime ToDateTime(string value)
{
  if (value == null)
    return new DateTime(0L);
  else
    return DateTime.Parse(value, (IFormatProvider) CultureInfo.CurrentCulture);
}

As you can see, this method use DateTime.Parse method with your CurrentCulture . 如您所见,此方法将DateTime.Parse方法与CurrentCulture And if your string doesn't match your current culture date format, your code will be broken. 如果您的字符串与您当前的文化日期格式不匹配,您的代码将被破坏。 That's the reason you get this error. 这就是你得到这个错误的原因。

Just curious on why the same code, but there is an error happen in my real machine. 只是好奇为什么相同的代码,但在我的真机中发生了错误。 Is that I miss out to configure something? 我错过了配置的东西吗?

/ seperator has a special meaning of " replace me with the current culture's date separator " / seperator具有“ 用当前文化的日期分隔符替换我 ”的特殊含义

Probably your virtual machine and real machine have different culture and that's why they have different date seperators . 可能你的虚拟机和真机有不同的文化,这就是为什么他们有不同的日期分隔符

DateTime dt = DateTime.Now; // get current date time
txtFormat.Text = string.Format("{0:yyyy/dd/MMM hh:mm:ss}", dt); // you can specify format according to your need

Format can be such as dd/mm/yy dd/mmm/yyyy mmm/dd/yy mm/dd/yy mm/dd/yyyy 格式可以是dd / mm / yy dd / mmm / yyyy mmm / dd / yy mm / dd / yy mm / dd / yyyy

Note : you can use any separator in format . 注意:您可以使用格式的任何分隔符。

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

相关问题 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 迄今为止的字符串解析错误“字符串未被识别为有效的DateTime。” - String to date parsing error “String was not recognized as a valid DateTime.” 无法将字符串识别为有效的DateTime。 :仅服务器错误 - String was not recognized as a valid DateTime. : Error only in Server 对于某些确切的日期,发生了“字符串未被识别为有效的DateTime。”错误 - "String was not recognized as a valid DateTime.” error occurs for some exact dates 错误“字符串未被识别为有效的日期时间。” 在 c# - error “String was not recognized as a valid DateTime.” in c# 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.' 无法将字符串识别为有效的DateTime。 引发异常 - String was not recognized as a valid DateTime. Throws an Exception 字符串未被识别为有效的日期时间。 将字符串转换为日期时间 - String was not recognized as a valid DateTime. Converting string to DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM