Next Occurrence of a Weekday
If you need to produce the next occurrence of a weekday corresponding to a given
event date, you can rely on techniques that are similar to those I presented
for last occurrence. You might think that to produce the next occurrence of
a weekday, all you need to do is add 7 days to the calculation of the last occurrence
of that weekday. But the calculation is trickier than you think. To make the
calculation of the next occurrence inclusive, you need to add 7 days
to the exclusive calculation of the last occurrence. Think about it:
If today is Tuesday, the inclusive calculation of the last occurrence of Tuesday
will yield today's date. When you add 7 days, you end up with a Tuesday date
a week ahead of today—not with today's date. However, if you use the
exclusive calculation of last Tuesday's date, you will get the Tuesday date
a week ago. Adding 7 days gets you today's date, effectively making the calculation
of the next occurrence of a weekday inclusive. Similarly, to make the calculation
of the next occurrence exclusive, you need to add 7 days to the inclusive calculation
of the last occurrence.
To clarify this idea, Figure 1
shows a few examples, assuming today's date is December 19, 2006 (Tuesday).
As you can probably figure out, instead of adding 7 days, you can add (or subtract)
any multiplication of 7 days to get the next/last occurrence of a weekday several
weeks ahead/ago.
Messing with Your Head
You might think my goal is to mess with your head. Unfortunately, there are
no built-in functions that perform such calculations, and the techniques I'm
sharing are as simple as you can get. Because calculations such as the last/next
occurrence of a weekday are frequently needed, it's good to be familiar with
the techniques I've presented. But we're not done yet. Next month, I'll discuss
datetime-related calculations further. If you think the calculations in this
article were tricky, just wait. In the meantime, get involved! Do you have datetime
solutions you'd like to share? Check out the sidebar "Share Your DATETIME Thoughts"
for some great reader responses.
End of Article
Prev. page
1
2
[3]
next page -->