Datetime 转换失败【非常邪门】
星际 2011-08-01 04:50:45 大家好,目前遇到一个邪门的问题,环境:.Net Framework 2.0 + Windows 2000
以下是测试程序做的代码片段:
textBox1.AppendText(DateTime.Now.ToString() + Environment.NewLine);
textBox1.AppendText("Try Convert datetime:2011/8/1 14:22:30"+Environment.NewLine);
try
{
textBox1.AppendText(Convert.ToDateTime("2011/8/1 14:22:30").ToString() + Environment.NewLine);
textBox1.AppendText("Try Convert datetime successfully" + Environment.NewLine);
}
catch(Exception ex)
{
textBox1.AppendText("Try Convert datetime has error:" + ex.ToString() + Environment.NewLine);
}
输出结果:
2011/08/01 16:44:03
Try Convert datetime:2011/8/1 14:22:30
Try Convert datetime has error:System.IndexOutOfRangeException: 索引超出了数组界限。
在 System.Globalization.DateTimeFormatInfo.InsertHash(TokenHashValue[] hashTable, String str, TokenType tokenType, Int32 tokenValue)
在 System.Globalization.DateTimeFormatInfo.CreateTokenHashTable()
在 System.Globalization.DateTimeFormatInfo.Tokenize(TokenType TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
在 System.__DTString.GetSeparatorToken(DateTimeFormatInfo dtfi)
在 System.DateTimeParse.Lex(DS dps, __DTString& str, DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result, DateTimeFormatInfo& dtfi)
在 System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles, DateTimeResult& result)
在 System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
在 System.DateTime.Parse(String s, IFormatProvider provider)
在 System.Convert.ToDateTime(String value)
在 TestTime.Form1.button1_Click(Object sender, EventArgs e)
为什么时间格式不能被识别?查看了区域设置,都是选择的中国。邪门了。。。