解决方案
感谢 casperOne's answer ,这是我的结果函数:
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
我正在读取一个数据库,该数据库以字符串格式 hh:mm 保存时间信息。出于某种原因,我记得很久以前用来完成的一个内置函数,但我一辈子都不记得如何做到这一点。因此,我编写了这个快速而肮脏的函数:
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 点?”问题的答案:
How to convert 1400-1500 to 2pm - 3pm?
在格式中,将格式中的大写 H 替换为小写 h 以切换为 12 小时格式。
关于vb.net - 将表示 24H 时间的字符串转换为 12H 时间 VB.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/687085/