【问题标题】:Classic ASP / VBSCRIPT 12 Hour Format from FormatDateTime()来自 FormatDateTime() 的经典 ASP / VBSCRIPT 12 小时格式
【发布时间】:2020-08-15 05:48:52
【问题描述】:

我当然会很感激一些帮助。我能够将我的时间从 2020 年 7 月 31 日上午 8:53:20 这样的值格式化为上午 8 点 53 分,并使用以下内容。

replace(replace(formatdatetime(formatdatetime(myRS("dateTime"),4)),":00 AM"," AM"),":00 PM"," PM")

但我认为没有更有效的方法吗?

【问题讨论】:

  • 您的代码不完整代表myRS ??
  • 如果您的方法有效并得到您所需要的,您实际上只需要一组replace(xxx, ":00 ", " "),这样您就不必费心检查上午和下午。

标签: vbscript asp-classic


【解决方案1】:
<%@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)
    
%>

示例输出:

07:45 PM
9:00 AM
02:55 PM

【讨论】:

    【解决方案2】:
    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
    

    如您所见,它会说 cmets。

    【讨论】:

    • 这对我有用,但有一件事。如果时间是第 1 个小时,我得到一个 00 表示 Hour (Now) 并且它不是 12:01 AM,而是 0:01 AM
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多