zod icon indicating copy to clipboard operation
zod copied to clipboard

[Date] Parsing of DateOnly strings

Open KennethHoff opened this issue 2 years ago • 2 comments

3.20 introduced z.string().DateTime() for cleaner string->DateTime parsing. However, it only supports strings containing both Date and Time

An API I'm working with includes only the Date part (today would be 2022-12-12). This API is written in C# using the DateOnly struct.

The built-in JavaScript new Date(<string>) does work with DateOnly strings, so I don't see a huge reason not to support it as well. I don't know what the API for it would be though, but the two most obvious ones that I could think of are the following:

  1. An additional option in the string().DateTime() function: dateOnly: boolean
  2. A separate string().Date() function (or string().DateOnly())

(As an aside, C# also has a TimeOnly struct, but I don't really see how that would be parseable into a JavaScript primitive)

KennethHoff avatar Dec 12 '22 18:12 KennethHoff

Small correction - z.string().datetime() still returns a string, not a Date. I'm in favor of adding .date() and .time() methods for convenience, though using .regex for these is pretty straightforward as well.

colinhacks avatar Dec 12 '22 19:12 colinhacks

I'm in favor of adding .date() and .time() methods for convenience.

Can I take a shot at these?

igalklebanov avatar Dec 13 '22 15:12 igalklebanov

Can I take a shot at these?

If you're asking for permission to submit a PR, please do. PRs are always welcome. Thanks.

JacobWeisenburger avatar Dec 25 '22 14:12 JacobWeisenburger

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 28 '23 04:03 stale[bot]

Not stale bro..

igalklebanov avatar Mar 29 '23 22:03 igalklebanov

Hey there @colinhacks!

Any ETA on the release of @igalklebanov's PR?

montedonioluiz avatar Apr 13 '23 18:04 montedonioluiz

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 12 '23 21:07 stale[bot]

Not stale bro.

igalklebanov avatar Jul 12 '23 22:07 igalklebanov

Any update on this?

KrisBierma avatar Jan 10 '24 21:01 KrisBierma