Validating date formats with regex
Regex examples for validating date strings such as dd.MM.yyyy HH:mm:ss and yyyy-MM-dd HH:mm in forms and text input.
If you accept dates as free text, you have to validate the format before saving. Below are regular expressions for two common formats: dd.MM.yyyy HH:mm:ss shown to users, and yyyy-MM-dd HH:mm used by databases and APIs.
They do more than count digits — they validate ranges: day 01–31, month 01–12, hour 00–23, minutes and seconds 00–59. Input such as “32.13.2026” is rejected before it reaches your code.
Note: a well-formed date may still not exist in the calendar (31.02.2026). Month lengths and leap years cannot be handled by a regular expression; verify those with a date parser.
dd.MM.yyyy HH:mm:ss format
^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.\d{4} (?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$yyyy-MM-dd HH:mm format
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) (?:[01]\d|2[0-3]):[0-5]\d$
Short guide
When to use it
Date-format regex is useful as a quick first check in forms that require a fixed date shape. Appointment screens, report filters, log searches and imports can all use it as an early guard.
What to watch
- Regex checks the shape; validate whether the date actually exists separately.
- Handle time zone and locale outside the regex.
- Before saving, try parsing the value into a real Date/DateTime object.
Common mistake
Trying to catch every impossible date, such as 31 February, with one regex makes the code harder to maintain. Parsing is usually better for that step.
Before using this in a project
Regex examples are useful shortcuts, but real projects should also consider the data source, line endings, browser/server differences and user input mistakes. In web forms, admin panels and custom software, validation should not live only on the frontend; the server side should check it too.