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.

JavascriptForm Validation

Code

javascriptsnippet
21 lines
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: document are 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

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.