FormatDateTime()的经典ASP/VBSCRIPT 12小时格式

FormatDateTime()的经典ASP/VBSCRIPT 12小时格式,vbscript,asp-classic,Vbscript,Asp Classic,我当然会感谢你的帮助。我可以通过以下方式将我的时间从7/31/2020 08:53:20 AM这样的值格式化为8:53 AM replace(replace(formatdatetime(formatdatetime(myRS("dateTime"),4)),":00 AM"," AM"),":00 PM"," PM") 但我认为还有一种更有效的方法,不?Set speech=CreateObj

我当然会感谢你的帮助。我可以通过以下方式将我的时间从7/31/2020 08:53:20 AM这样的值格式化为8:53 AM

replace(replace(formatdatetime(formatdatetime(myRS("dateTime"),4)),":00 AM"," AM"),":00 PM"," PM")
但我认为还有一种更有效的方法,不?

Set speech=CreateObject(“sapi.spvoice”)
Set speech = CreateObject("sapi.spvoice")
Speech.speak "Here's one way it sets the locale to New Zealand which is 12 hours  - it does NOT set time zone"
SetLocale("en-nz")
speech.speak now()
speech.speak "Here's another way" 'if locale is 12 hours it does nothing
Do
    If Hour(Now) < 12 then 
        Var = Hour(Now) & " AM"
    else
        Var = Hour(Now) - 12 & " PM" 
    End If

    speech.Speak Var &  " and " & Minute(Now) & " minutes and " & Second(Now) & " seconds"
    wscript.sleep 5
Loop
Speech.speak“这里有一种方法可以将地点设置为新西兰12小时-它不设置时区” SetLocale(“en nz”) 现在就说 speech.speak“这是另一种方式”如果语言环境是12小时,它什么也不做 做 如果小时(现在)<12那么 Var=小时(现在)和“上午” 其他的 Var=小时(现在)-12点和“下午” 如果结束 演讲。说Var&“and”&分钟(现在)&“minutes and”&秒(现在)&“seconds” wscript.sleep 5 环
正如您所见,它说出了注释。


<%@LANGUAGE="VBScript"%>
<%
    
    ' The actual LCID.
    
    Response.LCID = 2057
    Response.CodePage = 65001
    
    
    Function AM_PM_Time(TheTime, ZeroPad)
        
        Dim actual_LCID, time_formatted, time_split_1, time_split_2
        
        ' Make sure the the date or time being passed is valid.
        
        If NOT isDate(TheTime) Then Exit Function
                        
        ' Make a note of the actual LCID.
        
        actual_LCID = Response.LCID
        
        ' Change the LCID to 1033 so time is formatted as 00:00:00 AM/PM.
        ' If your LCID is already 1033 you can delete the LCID code.
        
        Response.LCID = 1033
        
        ' Format the current date / time. 
        
        time_formatted = FormatDateTime(TheTime,3)
        
        ' Split the time stamp.
        
        time_split_1 = Split(time_formatted,":")
        
        ' Split the AM / PM.
        
        time_split_2 = Split(time_formatted," ")
        
        ' Revert the LCID back to its original value.
        
        Response.LCID = actual_LCID
        
        ' [Optional]
        ' Zero-pad the first number of the timestamp, for example: "07:45 PM" rather than "7:45 PM"
        
        If ZeroPad Then
        
            If Len(time_split_1(0)) = 1 Then time_split_1(0) = cStr("0" & time_split_1(0))
        
        End If
        
        ' Output the newly formatted time.
        
        AM_PM_Time = time_split_1(0) & ":" & time_split_1(1) & " " & time_split_2(1)
            
    End Function
    
    Response.Write AM_PM_Time(Now(),True) & "<br>"
    Response.Write AM_PM_Time("2020-08-17 09:00:00",False) & "<br>"
    Response.Write AM_PM_Time("14:55:00",True)
    
%>
示例输出:

下午7:45
上午9:00
下午2:55


您的代码不完整,代表的是
myRS
?如果您的方法有效并满足您的需要,您实际上只需要一组
替换(xxx,“:00,”)
,这样您就不必费心同时检查AM和PM。这对我来说很有效,但有一件事。如果时间是第一个小时,我得到了一个小时的00(现在),它不是12:01 AM,而是0:01 AM