首先你需要确定rsprefobj("lastseentstamp")的数据类型:
MsgBox TypeName(rsprefobj("lastseentstamp"))
如果是字符串,需要先转换成日期时间值:
lastseenstatus = CDate(rsprefobj("lastseentstamp"))
如果您想根据系统的区域设置格式化日期,请使用FormatDateTime()函数作为@John建议:
MsgBox FormatDateTime(lastseenstatus)
如果无论系统的区域设置如何,您都需要不同的日期格式,您必须自己构建格式化字符串:
Function LPad(v) : LPad = Right("00" & v, 2) : End Function
Function FormatDate(d)
formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
LPad(Second(d))
If Hour(d) < 12 Then
formattedDate = formattedDate & " AM"
Else
formattedDate = formattedDate & " PM"
End If
FormatDate = formattedDate
End Function
MsgBox FormatDate(lastseenstatus)
或使用 .Net StringBuilder 类:
Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M\/dd\/yyyy h:mm:ss tt}", lastseenstatus
MsgBox sb.ToString()
不过,在我的测试中,我无法让 tt 格式说明符起作用,因此您可能不得不求助于这样的方法:
Set sb = CreateObject("System.Text.StringBuilder")
If Hour(lastseenstatus) < 12 Then
am_pm = "AM"
Else
am_pm = "PM"
End If
sb.AppendFormat_5 Nothing, "{0:M\/dd\/yyyy h:mm:ss} {1}", _
Array(lastseenstatus, am_pm)
MsgBox sb.ToString()