input.valueAsDate Reads Date Inputs As UTC Midnight
Read <input type="date"> through valueAsDate to get a UTC-midnight Date object, because the raw .value string has no timezone at all.
Code
const input = document.querySelector('#booking-start');
input.value = '2026-03-15';
// .value is the bare string the form submits — no timezone attached.
console.log('raw value:', input.value);
// valueAsDate returns a Date set to UTC midnight of that calendar day.
const picked = input.valueAsDate;
console.log('as Date:', picked.toISOString());
console.log('is UTC midnight:', picked.getUTCHours() === 0
&& picked.getUTCMinutes() === 0
&& picked.getUTCSeconds() === 0);
// Clearing the field makes valueAsDate null, not an invalid Date.
input.value = '';
console.log('cleared:', input.valueAsDate === null);
// Same-day arithmetic is exact because both sides share UTC midnight.
input.value = '2026-03-18';
const days = (input.valueAsDate - picked) / 86_400_000;
console.log('days between:', days);What runs, in order
One function carries all the logic: days.
Watch out for
- Everything it calls is documented as part of the language core.
- A browser is the runtime here:
documentare not available in Node.
Using it
The declared signature: const days = (input.valueAsDate - picked)
Look for the raw value:, as Date:, is UTC midnight: labels in the printed lines; each label stands for one step.
Statements in the spotlight
const input = document.querySelector('#booking-start');
const days = (input.valueAsDate - picked) / 86_400_000;
const picked = input.valueAsDate;
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.Timezone Offset FinderLook up the UTC offset, abbreviation, and local time for any IANA time zone at a given instant, and see whether the zone observes daylight saving.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- Does input.valueAsDate Reads Date Inputs As UTC Midnight run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.