Two months before 30 April is not 2 March: booking-window date maths in vanilla JS
Some places in Japan only take bookings inside a rolling window. Saihoji, the Moss Temple in Kyoto, is one: online booking opens "2 months before your visit" and closes at 23:59 Japan time the day before. I added a small
Some places in Japan only take bookings inside a rolling window. Saihoji, the Moss Temple in Kyoto, is one: online booking opens "2 months before your visit" and closes at 23:59 Japan time the day before. I added a small calculator for it to my free Japan ticket-drops calendar, and the first version had a bug that most of us have shipped at least once.
The naive version
function openDateNaive(visit) { // visit: Date at 00:00 UTC
const d = new Date(visit);
d.setUTCMonth(d.getUTCMonth() - 2);
return d.toISOString().slice(0, 10);
}
openDateNaive(new Date(Date.UTC(2027, 4, 31))); // "2027-03-31" fine
openDateNaive(new Date(Date.UTC(2027, 3, 30))); // "2027-03-02" wrong
openDateNaive(new Date(Date.UTC(2027, 7, 31))); // "2027-07-01" wrong
openDateNaive(new Date(Date.UTC(2028, 3, 30))); // "2028-03-01" wrong (leap year)
setUTCMonth does not clamp. "30 February" does not exist, so the Date object rolls the extra days into March. For a booking window that is the worst kind of wrong: the tool tells someone their date opens on 2 March, when it most likely opened on 28 February and may already be full.
The fix: do the month maths on numbers, then clamp
const pad = n => String(n).padStart(2, "0");
// days in month m (1-12) of year y
const dim = (y, m) => new Date(Date.UTC(y, m, 0)).getUTCDate();
function openDate(iso, months) { // iso: "YYYY-MM-DD"
let [y, m, d] = iso.split("-").map(Number);
m -= months;
while (m < 1) { m += 12; y--; }
const last = dim(y, m);
const clamped = d > last;
return { date: `${y}-${pad(m)}-${pad(clamped ? last : d)}`, clamped };
}
openDate("2027-04-30", 2); // { date: "2027-02-28", clamped: true }
openDate("2027-08-31", 2); // { date: "2027-06-30", clamped: true }
openDate("2028-04-30", 2); // { date: "2028-02-29", clamped: true }
openDate("2027-02-27", 2); // { date: "2026-12-27", clamped: false }
Two details matter here:
- Keep the
clampedflag and show it to the user. The venue never says what happens to a date that does not exist two months earlier. Clamping to the month end is the usual reading, but it is still a reading, so the page says so instead of pretending to know. -
new Date(Date.UTC(y, m, 0))is day 0 of the next month, which is the last day of month m. It is the one place where the rollover behaviour helps you.
The deadline: "23:59 JST the day before"
Japan has no daylight saving time, so JST is always UTC+9. You don't need a timezone library to get an absolute instant:
function addDays(iso, n) {
const [y, m, d] = iso.split("-").map(Number);
const o = new Date(Date.UTC(y, m - 1, d + n)); // day overflow IS what we want here
return o.toISOString().slice(0, 10);
}
function jstMs(iso, hhmm) {
const [y, m, d] = iso.split("-").map(Number);
const [h, min] = hhmm.split(":").map(Number);
return Date.UTC(y, m - 1, d, h - 9, min);
}
new Date(jstMs(addDays("2027-04-30", -1), "23:59")).toISOString();
// "2027-04-29T14:59:00.000Z"
Then Intl.DateTimeFormat with the visitor's own timeZone turns that instant into their wall-clock time. In New York that deadline is the morning of the same calendar day, which is exactly the kind of thing people get wrong when they do the conversion in their head.
Don't guess what isn't published
The official page gives the opening date rule but no opening hour. It's tempting to write "opens at 00:00 JST" because that is what many Japanese booking systems do. I didn't: the calculator shows the date only, and the calendar feed uses all-day events for the openings. A wrong time in a reminder is worse than no time, because people trust reminders.
The same goes for closures. Saihoji suspends its regular visits from 12 January to 26 February 2027 for winter maintenance, so the calculator checks the date against a list of closure ranges before it says anything about booking.
Tests worth writing
These are the cases that caught real bugs for me:
- 30 and 29 April -> 28 February (non-leap year)
- 30 April 2028 -> 29 February 2028 (leap year)
- 31 August -> 30 June, 31 May -> 31 March (31 to 31 must not clamp)
- 27 February -> 27 December of the previous year (year boundary)
- deadline instant = 14:59 UTC on the day before
- a date inside the closure range returns "closed", not "open"
The working calculator, with the closure dates and official sources, is here: https://hodoyoku.pages.dev/drops/saihoji-moss-temple
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.