带小时和分钟的 VBS 时间范围

VBS time range with hours and minutes

我需要 运行 基于 10:05 上午和 20:30 下午之间的时间范围的 vbs 脚本。这是我当前的代码:

If Hour(Now()) >= 10 OR hour(Now()) < 20 Then
   WScript.Echo "Time Range"

Else

   WScript.Echo "NOT Time Range"

End If

但我正在努力处理会议记录部分。 查看 VBS 的 Minute Function 文档对我来说不是很清楚。

使用 Time() 函数检索当前时间并检查时间文字,或者如果您需要使用字符串(例如从某处检索配置),您可以使用 CDate 函数检索时间

Dim startTime, endTime

    ' Using time literals    
    If InTime( #10:05#, #20:30# ) Then 
        WScript.Echo "In time range"
    Else
        WScript.Echo "NOT in time range"
    End If 

    ' Using strings
    If InTime( CDate("10:05") , CDate("20:30") ) Then 
        WScript.Echo "In time range"
    Else
        WScript.Echo "NOT in time range"
    End If 


' Logic moved to a function to avoid duplication in previous samples    
Function InTime( startTime, endTime )
Dim currentTime
    currentTime = Time()
    InTime = CBool( ( currentTime >= startTime ) And ( currentTime <= endTime ) )
End Function

已编辑以适应评论

OP 在检查时间范围时询问为什么 And 而不是 Or。虽然问题中的情况应该用 And 处理(当前时间必须大于开始时间 And 小于结束时间),但有一种情况Or 必须使用:当要检查的时间范围在午夜之前开始并结束时。如果是这样的话,条件就变了

Option Explicit 

Dim startTime, endTime

    ' Using time literals
    If InTime( #10:05#, #20:30# ) Then 
        WScript.Echo "In time range"
    Else
        WScript.Echo "NOT in time range"
    End If 

    ' Using strings
    If InTime( CDate("10:05") , CDate("20:30") ) Then 
        WScript.Echo "In time range"
    Else
        WScript.Echo "NOT in time range"
    End If 

    ' Check over midnight
    If InTime( #20:30# , #10:05# ) Then 
        WScript.Echo "In time range"
    Else
        WScript.Echo "NOT in time range"
    End If 

' Logic moved to a function to avoid duplication in previous samples 
Function InTime( startTime, endTime )
Dim currentTime
    currentTime = Time()

    If startTime <= endTime Then 
        ' If startTime <= endTime both are in same day 
        '   current time should be 
        '       greater than startTime 
        '       AND lower than endTime
        '
        '                        >----------------------------<
        '  0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
        '
        InTime = CBool( ( currentTime >= startTime ) And ( currentTime <= endTime ) )
    Else 
        ' startTime > endTime when both times don't belong to same day
        '   current time should be 
        '       greater than start time (towards midnight) 
        '       OR lower than end time (backwards to midnight)
        '
        '                         >---------|--------------------<
        '  13.14.15.16.17.18.19.20.21.22.23.0.1.2.3.4.5.6.7.8.9.10.11.12.
        '
        '  ----------------------<                             >---------
        '  0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
        '
        InTime = CBool( ( currentTime >= startTime ) Or ( currentTime <= endTime ) )
    End If 
End Function