0
votes

I am trying to set the time on my DateTimePicker value to "8:00 AM" on Form Load (using vb.net) but can't work out how to do it.

I have tried setting the value in the properties of my DateTimePicker to "8:00 AM" but on Form Load it keeps on reverting to the current time.

Current time displayed

enter image description here

I have also tried setting the values on Form Load but now get error

DateTimePicker.Value = Date.Now("8:00 AM")

The error I get based on my code above is:

Expression is not an array or a method, and cannot have an argument list.

I can't work out how to set this values.

Anyone got any ideas ?

3
If it is always going to be 8AM, then just get the date part of Date.Now and add 8:00AM as default time. - jitendragarg
please try something like DateTimePicker.Value = New DateTime(0, 0, 0, 8, 00, 0) also try this before it DateTimePicker.Format = DateTimePickerFormat.Time; and let me it works or not - pedram

3 Answers

3
votes
dtppick.Value = Date.Today.AddHours(8)

Today will give you the day with 00:00:00 as time. You can replace 8 with a variable too, which will help changing the code later.

2
votes

Try this code. it will work

 Dim dtval As String = "8:00 AM"
 dtppick.Value = Convert.ToDateTime(dtval)
0
votes

Hi thanks to all on your contributions

This solved the problem as suggested by Senthilkumar

I just changed dtppick.Value to DateTimePicker.Value to correspond with name of my control and it worked

 Dim Timeval As String = "8:00 AM"
 DateTimePicker.Value = Convert.ToDateTime(Timeval)

I tried the Suggestion made to set value like DateTime(0, 0, 0, 8, 00, 0) but did not work it returned an error: Additional information: Year, Month, and Day parameters describe an un-representable DateTime.

Thanks