C# “字符串未被识别为有效的日期时间。” Window 7 电脑环境出现错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19656975/
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
"String was not recognized as a valid DateTime." Error occur in Window 7 computer environment
提问by Panadol Chong
Good day All,
大家好,
Before this, I have a c# system in a VM with Microsoft Window XP. I have some code to convert string to date time, the following is part of my code :
在此之前,我在装有 Microsoft Window XP 的 VM 中安装了 ac# 系统。我有一些代码可以将字符串转换为日期时间,以下是我的代码的一部分:
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.
in line 397.
直到今天,我已经到了这部分,浏览器显示错误消息:
String was not recognized as a valid DateTime.
在第 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.
只是好奇为什么相同的代码,但在我的真机中发生错误。是我错过了配置的东西吗?好心提醒。
采纳答案by Kamil Budziewski
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
/
无论您将使用哪种文化,上面的代码都将使用日期分隔符解析日期
回答by John Woo
DateTime dt = DateTime.ParseExact(formatted, "M/dd/yyyy", null);
回答by Soner G?nül
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
. 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.
如您所见,此方法将DateTime.Parse
method 与您的CurrentCulture
. 如果您的字符串与您当前的文化日期格式不匹配,您的代码将被破坏。这就是您收到此错误的原因。
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"
/
分隔符具有特殊含义“用当前文化的日期分隔符替换我”
Probably your virtual machine and real machine have different culture and that's why they have different date seperators.
可能你的虚拟机和真实机器有不同的文化,这就是为什么他们有不同的日期分隔符。
回答by Sunil Acharya
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 .
注意:您可以使用格式中的任何分隔符。