Runtime Date validation
Validate values received from application code after they have been parsed into Date objects.
Zod · Function
z.date() creates a Zod schema for validating JavaScript Date instances. Add min() and max() to enforce lower and upper date boundaries.
z.date() creates a schema that accepts JavaScript Date instances. Use min() and max() to constrain the accepted date range, while safeParse() reports success or failure as data instead of throwing.
z.date(params?)| Name | Type | Description |
|---|---|---|
params | object | Optional configuration object; the documented example uses an error callback that receives the validation issue. |
A Zod schema that validates Date instances and supports date-bound refinements.
import { z } from "zod";
const DateSchema = z.date()
.min(new Date("1900-01-01"), { error: "Too old" })
.max(new Date("2100-01-01"), { error: "Too far in the future" });
const result = DateSchema.safeParse(new Date("2025-06-15"));
console.log(result.success); // trueCreates a date schema, constrains it to an inclusive range, and validates a JavaScript Date without throwing on failure.
Validate values received from application code after they have been parsed into Date objects.
Prevent timestamps outside a permitted historical or future range.
Return tailored messages for missing and invalid date inputs.
z.date() validates JavaScript Date instances. A Date object passes, whereas an ISO-formatted string fails direct validation.
Follow-up: How would you enforce a date range? Use .min() for the earliest allowed Date and .max() for the latest allowed Date.
Call min() with a Date to reject earlier values and max() with a Date to reject later values. Both methods can receive an error customization object.
Follow-up: How can a caller inspect failure without catching an exception? Use safeParse(), which returns a result containing success and either data or validation errors.
The schema can be configured with an error callback, which receives the issue and can choose messages based on the input. The documentation demonstrates a required-input message and an invalid-date message.
Follow-up: What should be used when the input is text rather than a Date? Choose a suitable string schema or transform/convert the input before applying z.date().
z.coerce.date()z.iso.datetime()z.codec()min()max()safeParse()