Skip to content

date ​

A Date object with a valid date.

Signature ​

ts
function date(msg?: Message): Schema<Date>

Parameters ​

ParameterTypeDescription
msgMessageCustom message. Replaces the default messages for all errors of this schema

Accepts ​

A Date instance whose getTime() is not NaN. new Date('abc') is rejected.

undefined, null and '' are treated as empty — see Empty values.

Error codes ​

CodeWhenDefault message
requiredThe value is emptyRequired
dateNot a Date or an invalid dateMust be a valid date

You can change the texts: see Messages & i18n.

Dates from JSON ​

JSON has no date type — dates come from the client as strings ("2026-10-02"). Use coerce.date, it creates a Date from a string or a number.

Example ​

Period ​

ts
const Period = object({
  from: coerce.date(),
  to: pipe(coerce.date(), check((v, { parent }) => v >= new Date(parent.from), 'End is before start')),
});

validate(Period, { from: '2026-01-10', to: '2026-01-01' }).errors; // { to: 'End is before start' }

See also ​