Algodevguy
Algodevguy

Reputation: 31

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

If I do this:

dateTimePicker1.Value = DateTime.Today;

The date gets set to today's date but the time gets set to 12:00:00 AM. I don't want the time to be changed.

Any help appreciated (I searched quite a bit for this)

Thanks

Upvotes: 0

Views: 2002

Answers (1)

jAC
jAC

Reputation: 5325

As the according MSDN-site states, DateTime.Today is:

An object that is set to today's date, with the time component set to 00:00:00.

This means you have to "save" your time before setting the new 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.

Upvotes: 1

Related Questions