Skip to content

Parsing a date #879

Answered by JacobWeisenburger
blocka asked this question in Q&A
Jan 24, 2022 · 5 comments · 8 replies
Discussion options

You must be logged in to vote

Since v3.20, this got way easier.

Let me know if this works for you?

Code updated on 2023-02-25. Thanks to @pilotmoon, for the idea.

const stringToDate = z.string().pipe( z.coerce.date() )
console.log( stringToDate.safeParse( '2023-01-10' ).success ) // true
console.log( stringToDate.safeParse( '2023-13-10' ).success ) // false
console.log( stringToDate.safeParse( null ).success ) // false

the dates section has been updated in the README
https://github.com/colinhacks/zod#dates

Replies: 5 comments 8 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@baloodevil
Comment options

@baloodevil
Comment options

Comment options

You must be logged in to vote
4 replies
@pke
Comment options

@JacobWeisenburger
Comment options

@pke
Comment options

@McGern
Comment options

Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
2 replies
@JacobWeisenburger
Comment options

@JacobWeisenburger
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
8 participants