LogoReturn to Home RegEx
Integrations
All integrations
AWS API
AWS Lambda
DynamoDB
Oracle
Redshift
Snowflake
GraphQL
Supabase
Twilio
Azure Blob Storage
Slack
SendGrid
Generic HTTP API
AWS S3
Stripe
Microsoft SQL
Salesforce
PostgreSQL
MySQL
MongoDB
HubSpot
Google Sheets
Google BigQuery
Firebase
Airtable
Integrations
About UI Bakery
Log in
Request UI Bakery demo
RegEx library
Email regex Python
Phone number regex Python
IP address regex Python
Date regex Python
URL regex Python
Numbers only regex (digits only) Python
UUID regex Python
Regex match words Python
ZIP code regex Python
GUID regex Python
Password regex Python
HTML regex Python
SSN regex Python
XML regex Python
Mac address regex Python
Street address regex Python

Date regex Python

Date regular expressions can be used to validate if a string has a valid date format and to extract a valid date from a string.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!
JavaScript
Python
Java
C#
PHP

Simple date regex (DD/MM/YYYY)

Below is a simple regex to validate the string against a date format (D/M/YYYY or M/D/YYYY). This however does not guarantee that the date would be valid. You can also replace \\/ with a separator you need.

"^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$"
Test it!
/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/

True

False

Enter a text in the input above to see the result

Example code in Python:

import re

# Validate date
date_pattern = "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$"
re.match(date_pattern, '12/12/2022') # Returns Match object

# Extract date from a string
date_extract_pattern = "[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}"
re.findall(date_extract_pattern, 'I\'m on vacation from 1/18/2021 till 1/29/2021') # returns ['1/18/2021', '1/29/2021']

ISO 8061 date regex (e.g. 2021-11-04T22:32:47.142354-10:00)

The ISO 8061 is an international standard for exchanging and serializing date and time data. For validating the format of ISO 8061 date and time and for extracting it a following regular expression could be used:

"^(?:\\d{4})-(?:\\d{2})-(?:\\d{2})T(?:\\d{2}):(?:\\d{2}):(?:\\d{2}(?:\\.\\d*)?)(?:(?:-(?:\\d{2}):(?:\\d{2})|Z)?)$"
Test it!
/^(?:\d{4})-(?:\d{2})-(?:\d{2})T(?:\d{2}):(?:\d{2}):(?:\d{2}(?:\.\d*)?)(?:(?:-(?:\d{2}):(?:\d{2})|Z)?)$/

True

False

Enter a text in the input above to see the result

Example code in Python:

import re

# Validate ISO date
date_pattern = "^(?:\\d{4})-(?:\\d{2})-(?:\\d{2})T(?:\\d{2}):(?:\\d{2}):(?:\\d{2}(?:\\.\\d*)?)(?:(?:-(?:\\d{2}):(?:\\d{2})|Z)?)$"
re.match(date_pattern, '2021-11-04T22:32:47.142354-10:00') # Returns Match object

# Extract ISO date from a string
date_extract_pattern = "(?:\\d{4})-(?:\\d{2})-(?:\\d{2})T(?:\\d{2}):(?:\\d{2}):(?:\\d{2}(?:\\.\\d*)?)(?:(?:-(?:\\d{2}):(?:\\d{2})|Z)?)"
re.findall(date_extract_pattern, '2017-05-23T15:02:27Z | WARN | Record not found\n2018-05-23T15:02:28Z | WARN | Project with the id ’53’ was not found') # returns ['2017-05-23T15:02:27Z', '2018-05-23T15:02:28Z']
Test it!

True

False

Enter a text in the input above to see the result

Notes on date string regex validation

While there are some regular expressions that allow more complex date validations, it is usually better to validate dates using special date and time libraries. For example, in Python datetime package can be used for these purposes. In this case, the validation will look like this:

try:
  datetime.datetime.strptime("1-2-2022", '%d-%m-%Y')
  print("Correct date string")
except ValueError:
  print("Incorrect date string")

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now