Use this file to discover all available pages before exploring further.
Dates in JavaScript are notoriously difficult to work with, and JSON doesn’t have a native date type. Express Zod API provides specialized schemas to handle dates correctly in both requests and responses.
When you return a Date object in JSON, it’s automatically converted to an ISO 8601 string by calling toISOString():
const response = { createdAt: new Date("2022-01-22") };JSON.stringify(response);// {"createdAt":"2022-01-22T00:00:00.000Z"}
Similarly, dates can’t be transmitted in JSON format in their original form - they must be strings. The built-in z.date() schema doesn’t handle these conversions automatically.
ez.dateIn() accepts several ISO 8601 date/time formats:
2021-12-31T23:59:59.000Z // Full ISO with milliseconds and timezone2021-12-31T23:59:59Z // ISO without milliseconds2021-12-31T23:59:59 // Local time without timezone2021-12-31 // Date only
Both ez.dateIn() and ez.dateOut() accept metadata that appears in generated documentation:
ez.dateIn({ description: "User's date of birth", examples: ["1990-01-15"],})ez.dateOut({ description: "When the resource was created", examples: ["2024-03-08T12:00:00.000Z"],})
You can add refinements to date schemas for additional validation:
import { ez } from "express-zod-api";import { z } from "zod";const bookingEndpoint = endpointsFactory.build({ input: z.object({ startDate: ez.dateIn(), endDate: ez.dateIn(), }) .refine( (data) => data.endDate > data.startDate, "End date must be after start date", ), // ...});
ez.dateIn().refine( (birthDate) => { const age = new Date().getFullYear() - birthDate.getFullYear(); return age >= 18; }, "Must be at least 18 years old",)
ez.dateIn().refine( (date) => { const now = new Date(); const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); return date >= thirtyDaysAgo && date <= now; }, "Date must be within the last 30 days",)
Ensure the date string follows one of the supported ISO 8601 formats. Timestamps (epoch milliseconds) are not supported - convert them to ISO strings first:
// Wrongconst timestamp = 1641000000000;// Right const isoString = new Date(timestamp).toISOString();