Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow time parsing to have optional seconds #3925

Open
GustavoOS opened this issue Dec 24, 2024 · 1 comment
Open

Allow time parsing to have optional seconds #3925

GustavoOS opened this issue Dec 24, 2024 · 1 comment

Comments

@GustavoOS
Copy link

Zod has the ability to parse time using z.string().time() for the format HH:mm:SS[.s+]
However, this time validation is not compatible with browser API that only holds HH:mm for
Is it possible to make the seconds part also optional in this parsing, so it gets easier to parse dates compatible with time input?

@GustavoOS
Copy link
Author

For the moment I'm using

  z
  .string()
  .regex(/\d{2}:\d{2}/, "Time format expected is HH:mm")
  .refine((str) => {
    const [hour, minute] = str.split(":").map((d) => parseInt(d));
    return (
      hour != undefined &&
      minute != undefined &&
      hour >= 0 &&
      hour < 24 &&
      minute >= 0 &&
      minute < 60
    );
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant