Mikkel Bang
Mikkel Bang

Reputation: 624

Only update time on datetimepicker, not date

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.

Upvotes: 0

Views: 3260

Answers (3)

Dev
Dev

Reputation: 1541

To update to the current time, try using

DateTime.Now.TimeOfDay 

Upvotes: 0

Faaiz Khan
Faaiz Khan

Reputation: 332

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

Upvotes: 0

Biri
Biri

Reputation: 7181

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);

Upvotes: 2

Related Questions