If you do not indicate the day, he seems to be suggesting today. (Itβs not safe if you write, say, time tracking software.)
If you only need a part of the time, you can analyze it as you already did, and then capture only a part of the time:
' Existing code: Dim dt2 As DateTime = DateTime.ParseExact("12:00:01 AM", "hh:mm:ss tt", _ Globalization.DateTimeFormatInfo.InvariantInfo) ' Now grab just the time: Dim ts2 As TimeSpan = dt2.TimeOfDay
It will be a TimeSpan instead of a DateTime , but if you don't really need it as a DateTime, TimeSpan is more suitable for something that is just hours / minutes / seconds, but not days.
(You can also try using TimeSpan.ParseExact in the first place, but it is not designed to handle AM ββ/ PM and will parse 12:00:01 as 12 hours. So you will probably need DateTime.ParseExact and then .TimeOfDay. )
If you really need to represent it as a DateTime - with a date, say 1/1/0001, you can always convert this TimeSpan back to DateTime manually:
Dim dt3 As New DateTime(1, 1, 1, ts2.Hours, ts2.Minutes, ts2.Seconds, ts2.Milliseconds)
Joe white
source share