The Date Math Behind a Due Date Calculator (Naegele's Rule in JavaScript)
Estimating a due date looks trivial, but the date math has more edge cases than you would expect. Here is how I think about it when building a calculator, with plain JavaScript examples.
Important: everything below produces an estimate. Only about 4–5% of babies arrive on their estimated date. Anyone using a due date should confirm it with their doctor or midwife.
Naegele's rule: LMP + 280 days
The classic method takes the first day of the last menstrual period (LMP) and adds 280 days (40 weeks). The rule of thumb version is "add one year, subtract three months, add seven days", which is nearly the same thing. The full explanation lives on the Due Date Calc Naegele's rule page.
const DAY = 24 * 60 * 60 * 1000; function dueFromLmp(lmp) { // lmp: "YYYY-MM-DD" const [y, m, d] = lmp.split("-").map(Number); const start = Date.UTC(y, m - 1, d); return new Date(start + 280 * DAY); } Notice Date.UTC. This is the first edge case.
Time zones and DST
If you build dates with new Date("2026-03-01") and add milliseconds, then format in local time, a daylight-saving change can shift the result by an hour and sometimes by a whole day on display. Work in UTC for the arithmetic, and format with toISOString().slice(0, 10) or timeZone: "UTC" in Intl.DateTimeFormat. Dates here are calendar days, not moments in time.
Adjusting for cycle length
Naegele's rule assumes a 28-day cycle with ovulation around day 14. If someone has a 32-day cycle, ovulation is likely later, so a common adjustment is to shift the result by cycleLength - 28 days.
function dueFromLmpAdjusted(lmp, cycleLength = 28) { const base = dueFromLmp(lmp); return new Date(base.getTime() + (cycleLength - 28) * DAY); } Validate the input. Cycles outside roughly 21–45 days are unusual enough that a calculator should warn the user rather than quietly trust the number.
Conception date
If the conception date is known, the estimate is conception + 266 days (38 weeks). It is the same idea with a different anchor.
const dueFromConception = (c) => new Date(c.getTime() + 266 * DAY); IVF transfer dates
IVF is more precise because the transfer date is known. A common approach subtracts the embryo age from 266 days:
- Day 5 blastocyst: transfer date + 261 days
- Day 3 embryo: transfer date + 263 days
Make embryo age an explicit parameter instead of hard-coding it.
Leap years
Because you add days, not months, leap years take care of themselves. Adding 280 days to 2027-12-01 lands correctly across 2028-02-29 without special code. The bug appears when people use "add 9 months and 7 days" with setMonth, which can overflow on dates like January 31. Prefer day arithmetic and test it.
Gestational age as a bonus
Once you have the LMP you can compute the current week and day: Math.floor(days / 7) and days % 7. Clamp it to 0–42 weeks so you don't show nonsense for old dates.
Testing checklist
- LMP on Jan 31, Feb 28, Feb 29 and Dec 31
- Users in UTC+5:30 and UTC-8 (same input, same output)
- Cycle length of 21, 28, 35 and 45 days
- Future LMP dates (reject them)
- IVF day 3 versus day 5
Wrapping up
Good date code is mostly about picking one canonical representation, UTC calendar days, and refusing to mix in local-time logic. Whatever your calculator outputs, present it as an estimate and tell users to confirm with their healthcare provider.