Stupid Date Tricks

In this article, Doug Steele demonstrates a useful formula to use with date calculations.

If I’ve got a date, can I determine the date of the Monday for the week that date is in? For instance, I’ve got a date of 17 November 2004 (which is a Wednesday), and I want to identify that the Monday of that week is 15 November 2004.

In my September column (which dealt with calculating working days), I alluded to how the Weekday function works, but I’m not sure I really spelled out the details.

Given a date, the Weekday function returns a value between 1 and 7 to represent what day of the week the date represents. There’s an optional second parameter (FirstDayOfWeek) that you can pass to the function, though, that changes what day each of the numbers 1 through 7 actually represents.

By default, the Weekday function assumes that the week starts on Sunday, so that Sunday will be day 1, Monday will be day 2, and so on up to Saturday as day 7. You can change that behavior, though. If you want the week to start on Monday, you pass vbMonday (which equals 2) to the function as well as your date, and now Monday will be identified as day 1, Tuesday as day 2, and so on to Saturday as day 6 and Sunday as day 7. This can be very useful.

Because you want to calculate the Monday before a particular date, you’ll need to subtract a certain number of days from that date. If the date is a Monday, you don’t need to subtract anything. If the date is a Tuesday, you need to subtract a day, and so on. That means that a function like this will solve your problem:

However, I think it would be better to come up with a more generic function than this. For one thing, you may not always want to know the previous Monday–sometimes you may want, for instance, the previous Saturday or Sunday. As well, sometimes you may want a following day. For instance, if you have a given date, you may want to know the Monday before it and the Friday after your date.

Implementing the first option should be rather obvious: All you need to do is substitute a different value for FirstDayOfWeek in the call to Weekday in the function.

To determine the particular day after the given date, you’ll need to add a certain number of days to the date. It turns out that the number of days that you want to add is 7 days more than whatever you subtracted in the first case, or 8 – Weekday(…). However, there’s a potential problem with this solution. When I calculated the previous Monday earlier, if the date being checked was a Monday, the function simply returned the date itself. Assuming that I want the same behavior when determining the following date, I’d expect that if I ask for the Friday following a given date and I pass a Friday to the function, then I’ll get my date. That means that I want to use 0 instead of 7 when it’s 7 that will be returned by my calculation.

Fitting it all together, the following function does everything I want:

To get the previous Monday to a particular date, call the function as:

To get the following Friday, call the function like this:

If you’d rather the function return the date a week previous when the given date is the weekday in question, first find this line:

and then change it to:


 Your download file is called   Steele_Stupid_date_tricks.accdb


Other Pages On This Site You Might Like To Read

Forms, Sorting Reports, and Dating Errors
Access Answers: Sounds Good to Me 
What Every Access Developer Needs to Know About Word
Peter Comes to Bat – Handling Quotes, Weekdays

About Doug Steele

Doug Steele has worked with databases-both mainframe and PC-for many years with a major international oil company. In a previous life, he taught introductory computer programming at the University of Waterloo. Microsoft has recognized him as an Access MVP for his contributions to the user community over the years.

Doug is the author of Access Solutions with Arvin Meyer.

This entry was posted in VBA. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.