recurring item on the same day

How to lock weekdays in a recurring schedule?

Pick the nearest date with the same weekday

Christiaan Huizer

--

Ever noticed how scheduling something “monthly” can be a bit unpredictable? It all comes down to the days of the week. Weeks are neat and tidy — add a week (or two), and you land on the same day you started. But months? They’re a bit more chaotic with their varying lengths.

In my last post, we tackled calculating future dates for recurring tasks. This time, we’ll dive into how to keep those tasks locked to the same day of the week. Why? Because, let’s face it, remembering tasks is way easier when they have a consistent weekly home.

Step 1 — check if we are good to go

First, we’re going to reuse some code we wrote earlier (check out my previous post for the nitty-gritty details). This code generates a list of dates based on your recurrence pattern (like, say, every month for three months).

the code we developed earlier used as our starting point

A few words on the ForEach() you see in the screenshot generating a list of dates. Instead of treating that list as one big blob, we want to look at each date inside it individually. To emphasize this, we give the outcome of the loop a singular name (here “theDate”) instead of a plural name (like “theDates”). This highlights that we’re evaluating each date one by one, like a careful inspection of each item in a collection. This is key to understanding how we can apply the SwitchIf() later on. If the weekday matches our start date’s weekday, we keep it If not, it’s tossed out and we have to do something else with it.

And that’s step one! In the next step, we’ll introduce a calculation to pinpoint the nearest date that matches our weekday criteria.

Step 2 — creating a list of dates to evaluate

Now we need to make a decision. We’ve got our list of dates that fall on the correct weekday, but which one do we choose? Do we want the first matching date after our initial target date? Or the one before?

Actually, neither! We want the date that’s closest to our target date, whether it falls slightly before or after. To do this, we create a small window of dates — using the weekday property, that is the position of a weekday in a week. Then, we scan this window for a date that matches our weekday.

I wrote about this technique in this blog post almost two years ago.

getting a list of dates

To make sure we haven’t gone off course, we can compare this new list to the dates we calculated in the previous blog post. As you can see in the screenshot, the date ranges nicely surround those “perfect fitting” dates.

Now, we need to filter this list to pinpoint the exact weekday match we’re looking for.

checking if we are still on track using the perfect dates from our previous blog post

Step 3 — locking in the perfect date

Here’s the elegant solution:

  • If our original target date already happens to fall on the correct weekday, we simply grab that date and add the original start time to it. No need for any fancy calculations.
  • If the original target date doesn’t match our weekday, we turn to our trusty list of candidates. We filter this list, zeroing in on the date that matches the weekday of our start date. Once we have this perfect match, we attach the original start time to it.

And that’s it! We’ve successfully built a system that ensures our recurring tasks always land on the same day of the week, no matter how irregular those monthly cycles might be. It’s like having a personal scheduling assistant that understands the importance of weekday consistency.

I hope this solution helps you tame those unruly recurring tasks!

our solution

I hope this article was informative and helpful. Did it help you to solve a problem you unlike would have solved other ways? What about a donation?

My name is Christiaan, and I regularly blog about Coda. While this article is free, my professional services (including consultations) are not, but I’m always happy to chat and explore potential solutions. You can find my free contributions in the Coda Community and on X. The Coda Community is a fantastic resource for free insights, especially when you share a sample doc.

--

--

No responses yet