-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdateValidator.py
More file actions
47 lines (40 loc) · 1.34 KB
/
dateValidator.py
File metadata and controls
47 lines (40 loc) · 1.34 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
36
37
38
39
40
41
42
43
44
45
46
47
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
"""
# Let TypeError be.
# This regex string will validate dates of type `dd/mm/yyyy`, `dd-mm-yyyy` or `dd.mm.yyyy`
# from years 1900 - 2049. Leap year support included. Original Regex string based on
# https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy
# modified to confine the year dates.
if re.match(
r"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1"
+ r"|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2"
+ r"))(?:(?:1[9]..|2[0][0-4].))$|^(?:29(\/|-|\.)0?2\3"
+ r"(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]"
+ r"|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4"
+ r"(?:(?:1[9]..|2[0][0-4].))$",
date_string,
flags=0,
):
return True
else:
return False