forked from santosjorge/cufflinks
/
date_tools.py
62 lines (51 loc) · 1.49 KB
/
date_tools.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import datetime as dt
def getDatefromDate(date,delta,strfmt='%Y%m%d'):
if type(date)==str:
date=stringToDate(date,strfmt)
return (date + dt.timedelta(delta)).strftime(strfmt)
def getDateFromToday(delta,strfmt='%Y%m%d'):
""" Returns a string that represents a date n numbers of days from today.
Parameters:
-----------
delta : int
number of days
strfmt : string
format in which the date will be represented
"""
return (dt.date.today() + dt.timedelta(delta)).strftime(strfmt)
def stringToDate(stringDate,strfmt='%Y%m%d'):
""" Converts a string format date into datetime
Parameters:
-----------
stringDate : string
date in string format
strfmt : string
format in which the input date is represented
"""
return dt.datetime.strptime(stringDate,strfmt).date()
def intToDate(intDate):
""" Converts an int format date into datetime
Parameters:
-----------
intDate : int
date in int format
Example:
intDate(20151023)
"""
return stringToDate(str(intDate))
def dateToInt(date,strfmt='%Y%m%d'):
""" Converts a datetime date into int
Parameters:
-----------
date : datetime
date in datetime format
strfmt : string
format in which the int date will be generated
Example:
dateToInt(dt.date(2015,10,23),'%Y')
"""
return int(date.strftime(strfmt))
def dateToString(date,strfmt='%Y%m%d'):
return dt.datetime.strftime(date,strfmt)
def stringToString(date,from_strfmt='%d%b%y',to_strfmt='%Y%m%d'):
return dt.datetime.strftime(stringToDate(date,from_strfmt),to_strfmt)