设置DatePicker值 [英] Setting DatePicker Value
问题描述
我目前有一个程序,该程序从datePicker获取值,并将日期另存为字符串。我只需要日期而不是时间,因此我使用以下代码保存了日期值:
I currently have a program that takes the value from a datePicker and have the date saved as a string. I only needed the date not the time so i used the following code to save the date value:
DateTime StartDate;
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString()
我有 inMyString
推回到我的列表中,现在我想将其放回datePicker中。
I have the inMyString
pushedBack into my list and now i want to place it back into the datePicker.
在MSDN上,向我展示了以下示例
On MSDN is shows me the following example to set the date.
dateTimePicker1.Value = new DateTime(2001, 10, 20);
问题是我的日期选择器后面没有.Value(它不显示)
the problem is that having .Value after my date picker is not an option (it doesn't show in Intellisense.)
我也尝试过
datePicker1.SelectedDate.Value= new DateTime(inMyString)
并转换 inMyString
键入 DateTime
,但仍然无法正常工作。
and also converting the inMyString
to type DateTime
but it still does not work.
关于如何执行此操作的任何想法
任何建议和评论都将受到赞赏。
Any thoughts on how to do this?
Any Suggestions and comments are appreciated.
谢谢!
推荐答案
尝试一下:
datePicker1.SelectedDate = new DateTime(2001, 10, 20);
如果需要从字符串中获取日期时间:
If you need to take datetime from string:
datePicker1.SelectedDate = DateTime.Parse(inMyString);
侧面注:
Side note:
您可以替换这三行:
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString();
其中一个:
var inMyString = datePicker1.SelectedDate.Value.ToShortDateString();
另一面说明:不知道是否有是这样做的原因,但是您可以考虑将datetime存储为dattime,而不是字符串。
Another side note: don't know if there is a reason to it, but you might consider storing datetime as dattime, not as a string.
这篇关于设置DatePicker值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!