-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmd_date.py
More file actions
43 lines (36 loc) · 1.31 KB
/
md_date.py
File metadata and controls
43 lines (36 loc) · 1.31 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
import datetime
def extract_year(dateString):
if "-" in dateString:
if len(dateString) == 10: # YYYY-MM-DD format
return dateString[0:4]
elif len(dateString) == 5: # MM-DD format
return ""
def extract_month(dateString):
if "-" in dateString:
if len(dateString) == 10: # YYYY-MM-DD format
return dateString[5:7]
elif len(dateString) == 5: # MM-DD format
return dateString[0:2]
def extract_day(dateString):
if "-" in dateString:
if len(dateString) == 10: # YYYY-MM-DD format
return dateString[8:10]
elif len(dateString) == 5: # MM-DD format
return dateString[3:5]
def get_date(dateStr):
"""
Parse a date string of format `YYYY-MM-DD` or `MM-DD` into datetime object
"""
the_date = None
if "-" in dateStr:
if len(dateStr) == 10: # YYYY-MM-DD format
try:
the_date = datetime.datetime.strptime(dateStr, "%Y-%m-%d")
except:
print("invalid date: '" + str(dateStr) + "'")
elif len(dateStr) == 5: # MM-DD format
try:
the_date = datetime.datetime.strptime(dateStr, "%m-%d")
except:
print("invalid date: '" + str(dateStr) + "'")
return the_date