Convert string to date

cfisher440

Well-known member
Joined
Oct 11, 2005
Messages
73
Programming Experience
1-3
I have a string like this 11:23 that when I convert it to a datetime, it goes to this: 11:23:00 AM.
I want it to be a datetime but kept in the same format (11:23).
Any ideas would be appreciated.
 
Dim tm As DateTime = DateTime.Parse("11:23")
MsgBox(tm.ToShortTimeString)
 
DateTime objects don't have a format. Every DateTime object is stored as a binary value that contains all the information about that object. Format is only a factor when you want to display the object, i.e. when you are converting it to a string. JohnH's suggestion of using DateTime.Parse will absolutely work to parse the string provided into a DateTime object that will then contain all the information for a date of 01/01/0001, which is considered a "blank" date, and a time of 11:23:00 AM. If you want to display just the time in the format you specified in your first post then you would have to call ToLongTimeString or else ToString("h:mm:ss tt"), but the DateTime object itself doesn't know anything about this "format". It still contains all the date and time information.
 
actually jmcilhinney, DateTime.Parse("11:23") will set the date to todays.

... "It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time."
 
JohnH said:
actually jmcilhinney, DateTime.Parse("11:23") will set the date to todays.

... "It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time."
Good info. Thanks. :)
 
Back
Top