API input validation
Validate API payload timestamps before processing them.
Zod · Module
z.iso.datetime() validates strings against a strict subset of ISO 8601. Timezone offsets are rejected by default and can be enabled with { offset: true }.
z.iso.datetime() validates strings against Zod's supported ISO 8601 datetime format. It rejects timezone offsets by default, while the offset option enables them; the schema can then parse valid strings and reject invalid ones.
z.iso.datetime(options?)| Name | Type | Description |
|---|---|---|
options | object | Options object; the documented offset: true setting permits timezone offsets. |
A Zod ISO datetime string schema.
z.iso.datetime() for Zod's strict ISO 8601 datetime-string format rather than treating every ISO 8601 variant as valid.Z is accepted by the default schema.offset: true when inputs may contain timezone offsets.+02:00 is accepted when offset support is enabled.offset option is enabled.+02 or +0200 valid.import { z } from "zod";
const datetime = z.iso.datetime({ offset: true });
console.log(datetime.parse("2020-01-01T06:15:00+02:00"));
Creates an ISO datetime schema that accepts a timezone offset, then parses a matching string.
Validate API payload timestamps before processing them.
Require UTC-marked timestamps for persisted records.
Accept client timestamps that include explicit numeric offsets.
z.iso.datetime() validate by default?It validates an input string against Zod's supported ISO 8601 datetime format. The default form accepts a Z timezone marker but does not accept timezone offsets.
Follow-up: How would you accept an offset such as +02:00? Use z.iso.datetime({ offset: true }), while still rejecting the basic offset forms +02 and +0200.
Pass { offset: true } to the factory: z.iso.datetime({ offset: true }). This allows timezone offsets such as +02:00, and Z remains valid.
Follow-up: What happens to a local datetime such as 2020-01-01T06:15:00? It remains invalid because the documented schema requires a timezone designator.
A string with arbitrary fractional-second precision, such as .123456, is accepted by the documented examples. A timezone-less local datetime is not accepted by the default schema.
Follow-up: Can +0200 be used when offsets are enabled? No; the documentation specifically shows that only the colon form +02:00 is accepted among those examples.
z.iso.date()z.iso.duration()z.codec()z.date()