Title | Answer: Weekday(Date) Mod 6 |
Description | |
Keywords | quick question answer, quiz answer, answer |
Categories | |
|
|
1. What does this code do?
The Weekday function returns the day of the week number for a date. It returns 1 for Sunday, 2 for Monday, etc.
This routine takes the Weekday value Mod 6. The value Mod 6 is the same as the value for 1 through 5. If the Weekday value is 6, the Mod 6 result is 0. If the Weekday value is 7, the Mod 6 result is 1. This code determines whether the result is 1 so it is looking for cases where the Weekday value is 1 or 7. Those days are Sunday and Saturday. Thus the code is testing whether it's a weekend.
2. Is this a good idea?
No. This code is obscure and unnecessarily confusing.
3. Can you think of an alternative?
|
|
' See if it's a weekend.
If (Weekday(Date) = vbSunday) Or _
(Weekday(Date) = vbSaturday) _
Then
' It's a weekend.
' Do something ...
End If
|
|
This code is easier to read and understand. It uses standard Visual Basic constants vbSunday and vbSaturday to make it obvious what's happening.
Martijn Coppoolse has this suggestion:
Why not use Select Case?
|
|
' See if it's a weekend.
Select Case Weekday(Date)
Case vbSaturday, vbSunday
' It's a weekend.
' Do something ...
End Select
|
|
I find this more readable, because it prevents a clogging up of
brackets. If speed is an issue, this will also be faster since the
Weekday function will only have to be evaluated once. Especially in
cases where Date is different every time around, and the Weekday thus
can't be stored in a global variable...
Toby, realizing that the Weekday function can take the day you want to be the first day of the week as an optional parameter, proposed this solution:
|
|
' See if it's a weekend.
If Weekday(Date, vbMonday) > 5 Then
' It's a weekend.
' Do something ...
End If
|
|
4. How could you improve the original code?
Use comments to clarify what the code is doing. The original code I got this from used the following comment inside the If statement:
' It's a weekend
That's a fine comment. However, it's a little easier for the reader to see what's going on if there is also a comment before the If statement so the reader knows what the test is trying to do. The original version certainly needs a comment to explain the trick using Mod.
|
|
' If Weekday Mod 6 = 1, then it's Sunday
' (Weekday = 1) or Saturday (Weekday = 7).
If Weekday(Date) Mod 6 = 1 Then
' It's a weekend.
' Do something ...
End If
|
|
5. Now is this a good idea?
It's better but it still relies on a sneaky trick. The motivation behind the trick is that it is faster. In an informal test, the sneaky version took about 0.00005 seconds to decide whether it was a weekday. The improved code took 0.00010 seconds.
It's no coincidence that this is exactly twice as long. The most time consuming part of this code is calling the Weekday function and the improved version calls it twice. The following version calls Weekday only once and takes only about 0.00006 seconds. This is slightly longer than the sneaky version but not by much.
|
|
' See if it's a weekend.
week_day = Weekday(Date)
If (week_day = vbSunday) Or _
(week_day = vbSaturday) _
Then
' It's a weekend.
' Do something ...
End If
|
|
Once again the sneaky version is excused because it is faster, but how much do you care? Is your program likely to need to determine whether it is a weekend several thousand times? Probably not. If it does, you should check once when the program starts and save the results in a Boolean variable named g_IsWeekday or something. Later you can just check this variable without calling the relatively slow Weekday function. This method lets the program determine whether it's a weekday in only 0.0000002 seconds. The sneaky version takes 250 times as long!
Moral
Get it working correctly first. Then optimize only where you know it is necessary. (This is an important theme in my book Bug Proofing Visual Basic).
Remember that most software projects fail because they don't work correctly, not because they are too slow.
|