设置日期选择器值

发布于 2024-11-15 20:47:35 字数 702 浏览 2 评论 0原文

我目前有一个程序,它从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i 2024-11-22 20:47:35

试试这个:

datePicker1.SelectedDate = new DateTime(2001, 10, 20);

如果您需要从字符串中获取日期时间:

datePicker1.SelectedDate = DateTime.Parse(inMyString);

旁注:

您可以将这 3 行:替换

String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString();

为一行:

var inMyString = datePicker1.SelectedDate.Value.ToShortDateString();

另一旁注:不知道是否有原因,但您可能会考虑将日期时间存储为 dattime,不是作为字符串。

Try this:

datePicker1.SelectedDate = new DateTime(2001, 10, 20);

If you need to take datetime from string:

datePicker1.SelectedDate = DateTime.Parse(inMyString);

Side note:

You can replace those 3 lines:

String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString();

with one:

var inMyString = datePicker1.SelectedDate.Value.ToShortDateString();

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.

想挽留 2024-11-22 20:47:35

如果您想在 WPF C# 中显示今天的日期,请使用以下方法:

datePicker1.SelectedDate = DateTime.Today.AddDays(-1);

If you want to show today's date in WPF C#, use this method:

datePicker1.SelectedDate = DateTime.Today.AddDays(-1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文