How to Coda with weeks?
This week, last week or next week?
Weeks have 7 days, always. That makes them rather easy to work with in Coda, but on the otherhand we have only a few week related functions. There is the Weekday()
, the position of the day in the week. By default in Coda, Sunday is the first day of the week, while in Europe we start counting at Monday. Using IsoWeekday()
makes Monday the first day of the week or your settings define how Weekday()
operates (although I consider this confusing, having a function that relates to settings….). The weekdayName()
is great when your native tongue is English and weekNumber()
again relates to the week order according USA standards, while IsoWeekNumber()
is roughly speaking for the rest of the world. There is also Workday()
, I wrote about this one in other blogs and I believe need an update. There is no native function published yet to deal with ‘thisWeek
’, although it is not difficult to find. in this blog we show the functions related to:
- this week
- last week
- next´week
We assume you use the Coda default settings, if not don’t worry it will work as well, but you won’t have Sunday as the first day of the week.
This Week
Getting all dates from this week is not too difficult. First the solution and below I break it down into parts. This helps to see the other two options.
Step 01 — Position
Ány date is a day with a number, number 1–7. Sunday is in our logic number 1 and Saturday number 7. We ask for the position of your date via Weekday()
.
Step 02 — a list of dates
This is maybe the most tricky part of most readers. The first part of the Sequence() gives us the first date of the week.
Well, the last date of the week we get via the second part:
The implicit logic we apply is that when you add a numer to a date is counts as a day, not as a month or a year, always as a day. We have a date, we ask for the position of that day in the week. We look for the first by going back by the day value plus one and next we find the last date the same way but this time we add seven. Last we take all the dates in between as numbers as you can see below.
Step 03 — ToDate()
This is actually the easiest step. We take the list of numbers and format them into dates we are use to. The ForEach()
function applies the function ToDate()
to every item in the list (of 7 numbers). You need to see this to understand how easy the next step is.
Last Week & Next Week
Once you see how the ForEach()
permits to apply a function to each individual item in the list we have our solution for the last & next week.
we simply go per item in the list 7 days back and for next week we go 7 days forward. Again we make use of the implicit logic that when you add a number to a date, you add a day or days, in our case we add 7 days to get next week.
We can do this all with standard Coda functions and it is not too difficult as you see. However I believe that Coda should offer these calculations as native functions like thisWeek()
, lastWeek()
and nextWeek()
. The above code snippets show how easy such a set up look like.
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.