question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for date, time and date-time format strings

See original GitHub issue

Currently, 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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:18
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

33reactions
colinhackscommented, Oct 14, 2020

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.

13reactions
devinhalladaycommented, Oct 20, 2021

Any updates on this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found