VB 2008 convert string "08:30:00" to datetime "08:30:00"
I'm working in Visual Studio 2008.
I have a string input in the format "hh:mm:ss" which I want to convert into a dateTime i开发者_C百科n the same format.
I have used the Convert.ToDateTime() function which converts it but adds a date to it as well. Is there a way to convert it without adding the date?
scanStartString = "08:00:00"
scanStart = Convert.ToDateTime(scanStartString)
'scanStart = "16/05/2011 08:00:00"
'required "08:00:00"
Cheers, Cap
No. Try TimeSpan instead:
Dim time = TimeSpan.Parse("08:00:00")
Use TimeOfDay - it will return TimeSpan
scanStart.TimeOfDay;
Just add the TimeOfDay method onto your convert line...
scanStart = Convert.ToDateTime(scanStartString).TimeOfDay
This worked for me:
Dim TimeOfDay As String = String.Format("{0:HH:mm:ss}", DateTime.Now)
TimeApp.LabelTime.Text = TimeOfDay
Simplest way to do this
Dim mydatetime As DateTime
mydatetime = Convert.ToDateTime(TextBox1.Text)
MsgBox(Format(mydatetime, "HH:mm:ss"))
精彩评论