I’ve been fighting for hours with datetime values. I now have the date values working right, but the times are still giving me trouble. Consider the following:
Private _pickupDate As DateTime
Public Property PickupDate As DateTime
Get
Return _pickupDate
End Get
Set(ByVal value As DateTime)
_pickupDate = value
txtPickupDate.Text = value.ToString("yyyy/MM/dd")
End Set
End Property
Public Property PickupTime As DateTime
Get
Return DateTime.Parse(ucPickupTime.Time)
End Get
Set(value As DateTime)
ucPickupTime.Time = value.ToString("HH:mm:ss")
End Set
End Property
Please Note: ucPickupTime is a user control that consists of 3 DropDownLists (Hours, minutes, and seconds). It’s Time property is a string that will always look like HH:mm:ss
Assume that I’m reading the datetime value 2012-10-26 16:00:00 from mysql.
When writing into the database, everything works smoothly, however, when I’m reading the time portion of the datetime, I’m getting back 04:00:00 (as opposed to 16:00:00) appearing in ucPickupTime.Time. Furthermore, when I read this into a gridview, it appears as 2012/10/26 04:00:00 PM
Obviously this is unacceptable as there’s a big difference between 4 AM and 4PM
I’ve tried a number of ways to correct this, but just haven’t been able to find the right one and I’m running out of ideas.
If anyone can suggest a way for me to keep the true time value (exactly as it is in the database) and put it into my control (or at least format it the way I want it), I’d really appreciate it.
Thanks in advance!
** EDIT **
I’ve updated the code above to reflect latest changes. This now formats the data correctly, but when updating or inserting a record into the database, the values appear as follows:
PickupDate | #12:00:00 AM# | Date
PickupTime | #10/4/2012 3:00:00 PM# | Date
What I’ve noticed looking closer at the data is that the date value used seems to always be the date on which the data was saved, not the date specified in txtPickupDate.Text
txtPickupDate.Text = value.ToString(“HH:mm:ss”)