I am trying to format a date string. My function currently generates "16:00". I want it to generate "4:00 PM". Here is my code:
Dim eventTime As String
eventTime = Trim(odbcReader("StartTime").ToString)
I tried this:
eventTime = Trim(odbcReader("StartTime").ToString("hh:mm tt"))
But it returns nothing. How do I get the date string to format properly?
You can't format a String
as a String
. If you want to format a time then you have to start with a time, ie a TimeSpan
value or a DateTime
value. You need to convert your existing String
to one of those types, then convert that to a String
in the desired format.
As you want a time of day, you need to create a DateTime
. A TimeSpan
just represents a period of time without reference to a start or end point, so it can't be directly formatted the way you want. You would need something like this:
Dim s1 = "16:00"
Dim t = DateTime.Parse(s1)
Dim s2 = t.ToString("h:mm tt")
Note that the format specifier is "h:mm tt", "hh:mm tt". You specifically said that you expect a result without a leading zero so you wouldn't provide a format specifier that would create a leading zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.