forked from codonlibrary/codonPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateValidator.py
More file actions
35 lines (29 loc) · 1.19 KB
/
Copy pathdateValidator.py
File metadata and controls
35 lines (29 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import re
def validDate(date_string: str)->bool:
"""
Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from
years 1900-9999. Leap year support included.
Parameters
----------
date_string : str
Date to be validated
Returns
----------
boolean
Whether the date is valid or not
Examples
---------
>>> validDate("11/02/1996")
True
>>> validDate("29/02/2016")
True
>>> validDate("43/01/1996")
False
"""
# This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy`
# from years 1900 - 9999. Leap year support included. Regex string from
# https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy
if re.match(r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:19|[2-9]\d)?\d{2})$", date_string, flags=0):
return True
else:
return False