Photo by Caitlyn de Wild on Unsplash

Next Birthday in Coda

something simple going wrong easily

Christiaan Huizer
4 min readMay 7

--

Below an easy to follow but nevertheless wrong solution to get the next birthday.

mistaken outcome

As you see, I make again use of the base function Date() as I did in my previous blog on dates, it avoids complications.

Although all looks fine, the provided code snippet does not deal with one exception: the leap year as you see below.

mistaken outcome

The year 2024 is a leap year and thus the outcome should be Feb 29, 2024. Instead it moved the year by one based on the birthday this year which is indeed March 01, 2023. Coda moves the date with one, because Feb 29 does not exist in 2023. When we add one year to this year, the outcome is both logical and mistaken.

What is a leap year?

A short intermezzo on the logic of leap years. In technical terms the year should be divisible by 4 or 400 but not by 100. In Coda terms:

getting the leap year

You can compare this syntax to what a Coda employee Justin Hales wrote a few years ago. Do you see the difference?


IF(

OR(
Remainder(thisRow.Year,400)=0,

AND(Remainder(thisRow.Year,4)=0,Remainder(thisRow.Year,100)!=0)

),"Leap Year", "NOT a Leap Year")

The same outcome with another way of writing (syntax). My guideline is that code should be easy to read and thus be understood. Only when we can read out loud what is happening, we are on to something. In a next blog I explore in more detail the meaning of a lower floor.

Although 29 feb can only exist in a leap year, the question is, do we need the leap year logic? The good news is, we don’t and that is because what I wrote earlier. In the Coda formula language the date is moved ahead by one when the date looked at, is not present. We take advantage of that behavior and shorten our formula using a simple solution.

The simple solution

The final solution is actually rather simple once you understand how to apply withName() . We generate the birthday for this year — indeed using Date() again — and the birthday for next year. Based on the actual date, the choice is presented.

correct next birthday date

No complexity with leap years only the choice between this year and next year. It is a short and elegant solution that keeps the base logic intact and does not work with unnecessary assumptions.

May it be of interest, we can use a slider via a controller, see below. This one helps to evaluate the assumptions made.

My name is Christiaan and I support SMB with calculations (budgets and — Human Resource — planning) and I prefer using Coda to get the job done. More about me below.

I hope you enjoyed this article. If you have questions feel free to reach out. Though this article is for free, my work (including advice) won’t be, but there is always room for a chat to see what can be done. Besides you find my (for free) contributions to the Coda Community and on Twitter.

Coda comes with a set of building blocks ー like pages for infinite depth, tables that talk to each other, and buttons that take action inside or outside your doc ーso anyone can make a doc as powerful as an app (source).

Not to forget: the Coda Community provides great insights for free once you add a sample doc.

Interested in a two way sync solution?

Check out this blog: it is easy and goes fast

--

--

Christiaan Huizer

I use Coda mainly for (HR) planning & (budget) calculations. Follow me to learn how to Coda with numbers. I blog at least once per week.