如果在 2 小时之间,跨越午夜

if between 2 hours, straddling midnight

我曾尝试在互联网上搜索此内容,但一无所获。

基本上,我有这个:

If Hour(Now()) >= 8 And Hour(Now()) < 17 then response.write("TEST")

这将在上午 8 点到下午 5 点之间显示“测试”一词 - 但我希望它能够跨越午夜。

我想说,如果时间在晚上 10 点到凌晨 4 点之间,那就显示一些东西。

我正在使用 Classic ASP - 有没有人可以帮助我 - 我要疯了!

目前,我只是将声明重复两次 - 就像这样;

If Hour(Now()) >= 22 And Hour(Now()) < 23 then response.write("TEST")
If Hour(Now()) >= 0 And Hour(Now()) < 4 then response.write("TEST")

这行得通,但是必须有一种方法可以做到这一点而不必执行 2 个 if 语句吗?

你可以试试

Dim h
    h = Hour(Now())
    If h >= 22 Or h < 4 Then Response.Write("Test")

或者

If Hour(DateAdd("h", 2, Now)) < 6 Then Response.Write("test")

或者

Select Case Hour(Now())
    Case 22,23,0,1,2,3 : Response.Write("test")
End Select

已编辑以适应评论

Option Explicit

WScript.Echo CStr(InTime("02:00", "18:00"))
WScript.Echo CStr(InTime("18:00", "22:00"))
WScript.Echo CStr(InTime("15:00", "04:00"))

Function InTime(ByVal startTime, ByVal endTime)
Dim thisTime
    thisTime  = CDate(FormatDateTime(Now(), vbShortTime))
    startTime = CDate(startTime)
    endTime   = CDate(endTime)
    If endTime < startTime Then endTime = DateAdd("h", 24, endTime)
    InTime = ( thisTime >= startTime And thisTime <= endTime )
End Function

好的 - 所以我不得不编写自己的函数:

function timeLimit(startTime, endTime)
    h=hour(now())
    if startTime>endTime then
        if h>=startTime or h<=endTime then
            timeLimit=True
        else
            timeLimit=False
        end if
    elseif startTime<endTime then
        if h>=startTime and h<=endTime then
            timeLimit=True
        else
            timeLimit=False
        end if
    else
        if h=startTime then
            timeLimit=True
        else
            timeLimit=False
        end if
    end if
end function

如果开始时间数字大于结束时间数字,这将有效 - 如果是,则它必须跨越午夜。然后它将采取相应的行动 - 返回 true 或 false,具体取决于输入。

所以,如果我想在晚上 8 点到 2 点之间显示 "Hello World" 字样,我会使用这个:

if timeLimit(20, 14) then Response.Write "Hello World"

如果我想在上午 8 点到下午 5 点之间显示 "Hello World",我会使用这个:

if timeLimit(8, 17) then Response.Write "Hello World"

如果我只想在下午 4 点显示 "Hello World",我会使用这个:

if timeLimit(16, 16) then Response.Write "Hello World"

希望对以后的人有所帮助。