Support for date, time and date-time format strings
See original GitHub issueCurrently, Zod is lacking support for validating date, time and date-time stamps. As a result, users may need to implement custom validation via .refine
. But the use case for date/time/date-time is very common, so if Zod ships it out-of-the-box like it does for uuid
, email
, etc, it would be helpful.
- Stemmed from https://github.com/vriad/zod/issues/30#issuecomment-679383286
- AJV’s in-built regexes are here for reference.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:19 (7 by maintainers)
Top Results From Across the Web
Standard date and time format strings | Microsoft Learn
A standard date and time format string uses a single character as the format specifier to define the text representation of a DateTime...
Read more >Convert string or numeric time values into date format ...
A date and time format string is a string of text used to interpret data values containing date and time information. Each format...
Read more >datetime — Basic date and time types — Python 3.11.1 ...
The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is...
Read more >Formatting and parsing dateTimes as strings - IBM
This section gives information on how you can specify the dateTime format using a string of pattern letters. When you convert a date...
Read more >Date and time formats used in HTML - MDN Web Docs
Dates and times in HTML are always strings which use the ASCII character set. Year numbers. In order to simplify the basic format...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
RFC for datetime methods
The hard problem here is naming the methods such that the validation behavior is clear to everyone. I’m opposed to adding a method called
.datetime()
since different people will have different ideas of what should be valid. I don’t like AJV’s datetime regex since it allows timezone offsets, which should almost never ever be used imo.Here is a set of method names I’d be happy with:
z.string().date()
—2020-10-14
z.string().time()
—T18:45:12.123
(T is optional)z.string().utc()
—2020-10-14T17:42:29Z
(no offsets allowed)z.string().iso8601()
—2020-10-14T17:42:29+00:00
(offset allowed)The longer method name
iso8601
makes the validation rule explicit. The documentation will clearly indicate that using UTC date strings is a best practice and will encourage usage of.utc()
over.iso8601()
.Milliseconds will be supported but not required in all cases.
Any updates on this?