设置日期选择器值
我目前有一个程序,它从 datePicker 获取值并将日期保存为字符串。我只需要日期而不是时间,因此我使用以下代码来保存日期值:
DateTime StartDate;
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString()
我已将 inMyString
推回到我的列表中,现在我想将其放回 datePicker 中。
在 MSDN 上向我展示了以下设置日期的示例。
dateTimePicker1.Value = new DateTime(2001, 10, 20);
问题是在我的日期选择器后面有 .Value 不是一个选项(它不会在 Intellisense 中显示。)
我也尝试过
datePicker1.SelectedDate.Value= new DateTime(inMyString)
并将 inMyString
转换为类型 DateTime
但还是不行。
关于如何做到这一点有什么想法吗?
如有任何建议和意见,我们将不胜感激。
谢谢!
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()
I have the inMyString
pushedBack into my list and now i want to place it back into the datePicker.
On MSDN is shows me the following example to set the date.
dateTimePicker1.Value = new DateTime(2001, 10, 20);
the problem is that having .Value after my date picker is not an option (it doesn't show in Intellisense.)
I have also tried
datePicker1.SelectedDate.Value= new DateTime(inMyString)
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
如果您需要从字符串中获取日期时间:
旁注:
您可以将这 3 行:替换
为一行:
另一旁注:不知道是否有原因,但您可能会考虑将日期时间存储为 dattime,不是作为字符串。
Try this:
If you need to take datetime from string:
Side note:
You can replace those 3 lines:
with one:
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.
如果您想在 WPF C# 中显示今天的日期,请使用以下方法:
If you want to show today's date in WPF C#, use this method: