jameslcs jameslcs - 1 year ago 124
Vb.net Question

DateTimePicker control, how to set time?

How to set time in a DateTimePicker control where i have already changed the display format = Time

I want to make the DateTimePicker to display time that i set.

e.g. set it to 5:30 PM

I try but fail using:

DateTimePicker1.Value = New DateTime(0, 0, 0, 5, 30, 0) 'fail at runtime

any idea, thanks

Answer Source

Try setting the date portion of the new DateTime to the current date:

DateTimePicker1.Value = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 5, 30, 0 )

Then, when you want to read the value just take the time portion of the DateTime. The date portion has to be set to some value but since you are only displaying the time it doesn't really matter what it is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download