将表示24小时时间的字符串转换为12小时时间VB.NET 解决方案

将表示24小时时间的字符串转换为12小时时间VB.NET 解决方案,vb.net,datetime,Vb.net,Datetime,感谢,以下是我的结果函数: 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.ToS

感谢,以下是我的结果函数:

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
 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
我正在读取一个以字符串格式保存时间信息的数据库,hh:mm。出于某种原因,我想起了一个我很久以前用来完成的内置函数,但就我的一生而言,我记不起如何去做。因此,我编写了这个quick n dirty函数:

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
 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

有人知道我想说的特定功能吗?

你可以看到关于如何将1400-1500转换为下午2点到3点的问题的答案,位于此处:


在格式中,您可以将格式中的大写字母H替换为小写字母H,以切换为12小时格式。

对不起,在意识到我没有在末尾添加问题后单击“发布”:p