-1

我在 xml 文件中写入日期,然后从 xml 文件中读取日期以显示。我已经使用Datetimepicker了 customFormat= MM/dd/yyyy。& 它设置<date>02/29/2001</date>在 xmlfile 中。

在读取值是否为"02/02/2001"它时,它会在 datetimepicker 中完美地读取并显示它

但如果值为"02/22/2001". 编辑

它引发异常。
字符串未被识别为有效的日期时间。

4

3 回答 3

2

该字符串"02/29/2001"实际上没有形成有效日期,因为 February 2001 只有 28 天,而您的字符串读取为February 29th, 2001,与January 32nd, 2001相同。

于 2012-02-08T12:00:35.803 回答
1
string val = "10/10/2010";
dateTimePicker1.Value = Convert.ToDateTime(val);
于 2012-02-08T11:23:13.333 回答
1

您是否将应用程序的文化信息设置为期望 MM/dd/yyyy 格式的日期?似乎期待 dd/MM/yyyy,这就是 02/02/2001 有效的原因,我怀疑 28/02/2001 也会有效。

编辑:等一下,2001 年不是闰年,29/02/2001 永远不会是有效日期!

编辑:添加示例

// C#
// Put the using statements at the beginning of the code module
using System.Threading;
using System.Globalization;
// Put the following code before InitializeComponent()
// Sets the culture to English (US)
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// Sets the UI culture to English (US)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

从这里: http: //msdn.microsoft.com/en-us/library/b28bx3bh%28v=vs.80%29.aspx 评论中的课程链接中的更多信息

于 2012-02-08T12:00:14.580 回答