Full Stack Engineer Guide: Leap-Year Safe Split Date Selectors & IANA Timezone Validation
Healthcare intake forms, travel booking flows, and financial KYC onboarding frequently use split Day / Month / Year dropdowns for birthdates and policy effective dates because native browser date pickers are awkward for scrolling back 50+ years. Yet naive split dropdowns allow users to select impossible calendar dates like September 31 or February 29, 2025, or shift a user's birth date by one day when converting a midnight new Date("1990-05-15") across negative UTC offsets. As a full stack engineer, here is how I build leap-year-aware split date selectors in React & TypeScript with strict Zod schema validation.
1. Dynamic Day Clamping & Leap-Year Calculation in React
Whenever the user changes the selected month or year, the Day dropdown must dynamically limit its options to the exact number of days in that month (accounting for the Gregorian leap-year rule: divisible by 4, except centuries not divisible by 400) and automatically clamp an out-of-range selected day:
// components/SplitDateSelector.tsx
import React from "react";
import { z } from "zod";
export function getDaysInMonth(year: number, monthOneBased: number): number {
if (monthOneBased === 2) {
const isLeap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
return isLeap ? 29 : 28;
}
return [4, 6, 9, 11].includes(monthOneBased) ? 30 : 31;
}
export const IsoCalendarDateSchema = z
.object({
year: z.number().int().min(1900).max(2100),
month: z.number().int().min(1).max(12),
day: z.number().int().min(1).max(31),
})
.refine(
({ year, month, day }) => day <= getDaysInMonth(year, month),
{ message: "Selected day does not exist in the chosen month and year." }
);
interface Props {
year: number;
month: number;
day: number;
onChange: (next: { year: number; month: number; day: number }) => void;
}
export const SplitDateSelector: React.FC<Props> = ({ year, month, day, onChange }) => {
const maxDays = getDaysInMonth(year, month);
const handleMonthChange = (nextMonth: number) => {
const clampedDay = Math.min(day, getDaysInMonth(year, nextMonth));
onChange({ year, month: nextMonth, day: clampedDay });
};
const handleYearChange = (nextYear: number) => {
const clampedDay = Math.min(day, getDaysInMonth(nextYear, month));
onChange({ year: nextYear, month, day: clampedDay });
};
return (
<div className="split-date-group" role="group" aria-label="Calendar Date">
<select value={month} onChange={(e) => handleMonthChange(Number(e.target.value))} aria-label="Month">
{Array.from({ length: 12 }, (_, i) => i + 1).map((m) => (
<option key={m} value={m}>{m}</option>
))}
</select>
<select value={Math.min(day, maxDays)} onChange={(e) => onChange({ year, month, day: Number(e.target.value) })} aria-label="Day">
{Array.from({ length: maxDays }, (_, i) => i + 1).map((d) => (
<option key={d} value={d}>{d}</option>
))}
</select>
<select value={year} onChange={(e) => handleYearChange(Number(e.target.value))} aria-label="Year">
{Array.from({ length: 110 }, (_, i) => 2026 - i).map((y) => (
<option key={y} value={y}>{y}</option>
))}
</select>
</div>
);
};
2. Storing Civil Dates vs. Instant Timestamps
Never serialize a civil date (like a date of birth) as a UTC timestamp (1990-05-15T00:00:00Z), which renders as May 14th in US timezones. Transmit civil dates strictly as ISO-8601 date-only strings (YYYY-MM-DD) and schedule appointments using an explicit IANA timezone identifier (e.g., Asia/Jakarta or America/New_York) alongside the UTC instant.