经典 ASP 与检测 day/time

Classic ASP with detecting day/time

我有下面的代码来检测用户点击网页的日期和时间。

dim time_hour
dim time_min
dim day_name
dim shouldShowNormal
dim showMsg

    time_hour = Hour(Now())
    time_min  = Minute(Now())
    day_name  = LCase(WeekdayName(Weekday(Now())))
    shouldShowNormal = true

    if (day_name = "thursday") then
        'Its Thursday so check the time
        if (Cint(time_hour) = 19 AND Cint(time_min) <= 59) then
            'Its between 19:00p-19:30p [7:00pm - 8:00pm]
            shouldShowNormal = false
        else
            'nothing is effected
            shouldShowNormal = true
        end if
    elseif (day_name = "wednesday") then
        'Its Wednesday so check the time
        if (Cint(time_hour) = 20 AND Cint(time_min) >= 30) then
            if (Cint(time_hour) = 21 AND Cint(time_min) <= 30) then
                'It's between 20:30p-21:30p [8:30pm - 9:30pm]
                shouldShowNormal = true
            else
                'nothing is effected
                shouldShowNormal = false
            end if
        else
            'nothing is effected
            shouldShowNormal = true
        end if
    elseif (day_name = "saturday") then
        'Its Saturday so check the time
        if (Cint(time_hour) = 17 AND Cint(time_min) <= 59) then
            'It's between 17:00p-18:00p [5:00pm - 6:00pm]
            shouldShowNormal = false
        else
            'nothing is effected
            shouldShowNormal = true
        end if
    else
        'nothing is effected
        shouldShowNormal = true
    end if

要检查的day/times如下:

Wednesday 7:00-8:00PM
Thursday 8:30-9:30PM
Saturday 5:00-6:00PM

这按需工作,但我想知道是否有更简单、代码更少的方法来做同样的事情?

你可以提取一个函数来检查当前时间是否在指定时间之间,然后你只需要检查日期+开放时间

因为根据您使用的函数,分钟不能超过 59,所以一个简单的函数就可以了(它实际上检查前 2 个参数是否超出范围,如果不超出范围,它 returns 真)。与上面的 if 语句相比,最后的检查相对简单

function IsBetween (curHour, curMinute, minHour, minMinute, maxHour, maxMinute)
    if curHour < minHour or curHour > maxHour or (curHour = minHour and curMinute < minMinute) or (curHour = maxHour and curMinute > maxMinute) then
        IsBetween = false
        Exit Function
    end if
    IsBetween = true
end function

dim time_hour
dim time_min
dim day_name
dim showNormal

day_name  = LCase(WeekdayName(Weekday(Now())))
time_hour = Hour(Now())
time_min  = Minute(Now())
' default to false, and only set it to true if we are in opening hours '
showNormal = false

If (day_name = "wednesday" And IsBetween(time_hour, time_min, 19, 0, 20, 0)) Or _
   (day_name = "thursday" And IsBetween(time_hour, time_min, 20, 30, 21, 30)) Or _
   (day_name = "saturday" And IsBetween(time_hour, time_min, 17, 0, 18, 0)) Then
    ' we are open :) '
    showNormal = true
End If

顺便说一句,你确定你的周三支票有效吗?似乎您在某个时间检查小时是否为 20,然后如果小时为 21,两者都不是真的吗? :)

Function IsNormalTime(dDateTime)
'// Default is true
IsNormalTime = True
'// Check day of week and set start/end times 
dim tStart, tEnd
select case WeekDay(dDateTime)
    case 4 '// Wednesday
        IsNormalTime = False
        tStart =  cDate("20:30:00")
        tEnd =  cDate("21:30:00")
    case 5 '// Thursday
        tStart =  cDate("19:00:00")
        tEnd =  cDate("20:00:00")
    case 7 '// Saturday
        tStart =  cdate("17:00:00")
        tEnd =  cDate("18:00:00")
    case else
        Exit Function
end select
'// Get time part only
dim dTimeOnly: dTimeOnly = dDateTime - FIX(dDateTime)
'// Check time against start/end times
select case true
    Case dTimeOnly < tStart, dTimeOnly >= tEnd
        exit Function
End Select
'// INVERT NORMAL TIME
IsNormalTime = Not IsNormalTime
END Function