我在下面有以下代码来检测用户点击网页的日期和时间。
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
检查的日期/时间如下:
Wednesday 7:00-8:00PM
Thursday 8:30-9:30PM
Saturday 5:00-6:00PM
这可以根据需要运行,但我想知道是否有更简单,更少的代码方式做同样的事情?
答案 0 :(得分:2)
你可以提取一个函数来检查当前时间是否在指定的时间之间,然后你只需要检查一天+开放时间
因为,从你正在使用的函数来看,分钟不能高于59,一个简单的函数会做(它实际检查前两个参数是否在边界之外,如果它们不是,它返回true)。与上面的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,那么两个都不是真的吗? :)
答案 1 :(得分:0)
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