Search code examples
c#datetimepicker

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


Solution

  • 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.