If you’ve ever had to build a calendar application, you know that dates are deceptively difficult. Dealing with time zones, leap years, and daylight savings is hard enough. But when you try to translate a legal document—like a custody agreement—into code, you enter a special circle of hell involving "floating" holidays and alternating year logic.
Most developers reach for a library, see that it doesn't quite cover "the third Monday of January in even years, unless it falls on a leap day," and realize they have to build a custom engine.
The Complexity of the "Nth-Weekday" Rule
Standard calendar events are easy. Birthdays happen on the same date every year. But legal holidays in the US and many other countries follow the "Nth-Weekday" rule. Think Memorial Day (last Monday in May) or Thanksgiving (fourth Thursday in November).
In a co-parenting context, the logic gets even more granular. A typical court order might state:
"The Father shall have the child on Thanksgiving in even-numbered years, and the Mother shall have the child in odd-numbered years, beginning at 9:00 AM on the Thursday and ending at 6:00 PM on the following Sunday."
To automate this, your backend needs to calculate not just the holiday, but the specific duration and the alternating toggle based on the year % 2 result. If the logic isn't perfect, you aren't just causing a bug; you're potentially causing a missed holiday for a parent and a child.
Handling the Edge Cases
When we were building the engine for CustodyTrac.com, we realized that simple recurrences weren't enough. We had to account for:
- The "Last" vs "Fourth" distinction: Some months have five Mondays. If a decree says "the last Friday," you can't just hardcode the 4th Friday.
- Handoff Buffers: Holiday logic rarely starts at midnight. It usually involves specific "wrap-around" times that conflict with the regular recurring schedule.
- Override Priority: The holiday schedule must always take precedence over the base custody rotation without deleting the underlying data for when the holiday ends.
Here is a simplified look at how one might approach calculating the "Nth weekday" in a given month:
/**
* Finds the Nth occurrence of a specific weekday in a month
* @param {number} year - e.g., 2024
* @param {number} month - 0-indexed (0 = Jan)
* @param {number} dayOfWeek - 0 (Sun) to 6 (Sat)
* @param {number} n - The occurrence (1st, 2nd, 3rd...)
*/
function getNthWeekday(year, month, dayOfWeek, n) {
let date = new Date(year, month, 1);
let add = (dayOfWeek - date.getDay() + 7) % 7;
date.setDate(1 + add + (n - 1) * 7);
// Ensure we haven't rolled into the next month
if (date.getMonth() !== month) return null;
return date;
}
Making it "Set and Forget" for Parents
The goal of building this automation wasn't just a technical challenge; it was about reducing conflict. For separated parents, every time they have to text an ex to ask, "Is it my turn for Christmas this year?", there is a chance for an argument.
By automating the alternating years and the nth-weekday rules, the calendar becomes the "source of truth." It removes the emotional weight of negotiation because the logic was agreed upon years ago and is now executed by code.
We decided to make these complex features—holiday rotations, expense tracking, and secure logs—available to everyone without a subscription. It’s a tool designed to provide stability in what is often a very unstable time.
If you’re a developer who has worked with complex scheduling logic or RRule sets, I’d love to hear how you handled these types of "legal-to-logic" translations.
Try it free →
https://custodytrac.com
Top comments (0)