StealthRT
StealthRT

Reputation: 10552

Classic ASP with detecting day/time

I have the following code below to detect the day and time when the user hits a web page.

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

The day/times to check is as follows:

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

That works as needed but I am wondering if there was an easier, less code way of doing the same thing?

Upvotes: 0

Views: 851

Answers (2)

JohnRC
JohnRC

Reputation: 1371

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

Upvotes: 0

Icepickle
Icepickle

Reputation: 12806

You could extract a function to check if the current time between the time specified, and then you only need to check the day + opening times

Since, from the functions you are using, minutes cannot go higher that 59, a simple function would do (it actually checks if the first 2 parameters are outside of the bounds, in case they are not, it returns true). The last check is then relatively simple compared to your above if statements

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

btw, are you sure your wednesday check works? it seems you check at a certain time if the hour is 20 and then if the hour is 21, both can't be true? :)

Upvotes: 2

Related Questions