简体   繁体   English

如何在不更改时间的情况下将datetimepicker上的日期设置为今天

[英]How do I set the date on the datetimepicker to today without changing the time

If I do this: 如果我这样做:

dateTimePicker1.Value = DateTime.Today; dateTimePicker1.Value = DateTime.Today;

The date gets set to today's date but the time gets set to 12:00:00 AM. 日期设置为今天的日期,但时间设置为12:00:00 AM。 I don't want the time to be changed. 我不想改变时间。

Any help appreciated (I searched quite a bit for this) 任何帮助表示赞赏(我为此进行了大量搜索)

Thanks 谢谢

As the according MSDN-site states, DateTime.Today is: 根据MSDN站点所述,DateTime.Today为:

An object that is set to today's date, with the time component set to 00:00:00. 设置为今天的日期且时间部分设置为00:00:00的对象。

This means you have to "save" your time before setting the new DateTime : 这意味着您必须在设置新的DateTime之前“节省”时间:

dateTimePicker1.Value = DateTime.Today.Add(dateTimePicker1.Value.TimeOfDay);

This now adds the time of your DateTimePicker to your date with the time 12:00:00AM / 00:00:00, meaning it gets set to the time before. 现在,这会将DateTimePicker的时间添加到日期为12:00:00 AM / 00:00:00的日期,这意味着它将被设置为之前的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM