views:

2362

answers:

1

SOLUTION

Thanks to casper, here is my resulting function:

Shared Function FormatDate(ByVal s As String) As String
    Dim DT As DateTime
    s = Regex.Replace(s, "[^1234567890]", "")
    DT = DateTime.ParseExact(s, "HHmm", Globalization.CultureInfo.InvariantCulture)
    Return DT.ToString("h:mm tt")
End Function

Much better :D


I am reading a database that holds time information in a string format, hh:mm. For some reason I am remembering a built-in function I used a long time ago to accomplish, but for the life of me I cannot remember how to do it. So instead, I wrote this quick n dirty function:

 Shared Function FormatDate(ByVal s As String) As String
    '' this function takes a time string from the database
    '' and changes it from 24h to 12h

    Dim oSplit As String() = s.Split(":"c)
    Dim oRet As String = "", suffix As String = ""
    Dim hour, minute As String

    If CInt(oSplit(0)) > 12 Then
        hour = CStr(CInt(oSplit(0)) - 12)
        suffix = "PM"
    Else
        hour = oSplit(0)
        suffix = "AM"
    End If
    minute = oSplit(1)
    oRet = String.Format("{0}:{1} {2}", hour, minute, suffix)
    Return oRet
End Function

Does anyone know of the particular function I am think I am referring to?

Thanks!

+4  A: 

You can see the answer posted to the question "How to convert 1400-1500 to 2pm - 3pm?", located here:

http://stackoverflow.com/questions/674269/how-to-convert-1400-1500-to-2pm-3pm/674320#674320

In the format, you substitute the capital H with a lowercase h in the format to switch to a 12-hour format.

casperOne
Pimp those answers, casper!
Randolpho
Thanks, this will get me started. cheers
Anders