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
26 lines (20 loc) · 867 Bytes
/
Copy pathdateValidator.py
File metadata and controls
26 lines (20 loc) · 867 Bytes
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
import re
def validDate(string: str):
"""Validates stringtype dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy` from years 1900 - 9999. Leap year support included.
Arguments:
string: str
Date to be validated
Outputs:
boolean
Whether the date is valid or not
>>> validDate("11/02/1996")
True
>>> validDate("29/02/2016")
True
>>> validDate("43/01/1996")
False
"""
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})$", string, flags=0):
return True
else:
return False