Reputation: 119
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
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 ?
Upvotes: 0
Views: 5169
Reputation: 119
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
Upvotes: 0
Reputation: 6472
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.
Upvotes: 3
Reputation: 2566
Try this code. it will work
Dim dtval As String = "8:00 AM"
dtppick.Value = Convert.ToDateTime(dtval)
Upvotes: 2