Question

Ok so I want to constantly update the time on my datetimepicker and I'm doing so as follows:

    private void timer1_Tick(object sender, EventArgs e)
    {
        dtpTime.Value = DateTime.Now;
    }

The interval for timer1 is set to 1000 (milliseconds). The problem I am having is I also display the date on my datetimepicker and I do not want to change the date with the update, but by assigning DateTime.Now to dtpTime.Value, it updates the entire datetimepicker. I simply cannot find a way to only update the time.

Thanks for the help in advance.

Was it helpful?

Solution

So you need to compose the value from the date of picker and the time of now?

DateTime now = DateTime.Now;

dtpTime.Value = new DateTime(dtpTime.Value.Year, dtpTime.Value.Month, dtpTime.Value.Day, now.Hour, now.Minute, now.Second);

OTHER TIPS

Set the CustomFormat property of the date picker to hh:mm:ss for time or dd/MM/yyyy hh:mm:ss for date and time

and select Custom under the Format Property of datetimepicker

dtpTime.Value = DateTime.Now;

It will display the value according to custom format, list of formats are available here at MSDN

Hope this helps..

To update to the current time, try using

DateTime.Now.TimeOfDay 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top