forked from WebJournal/journaldev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_date.py
More file actions
77 lines (56 loc) · 1.3 KB
/
python_date.py
File metadata and controls
77 lines (56 loc) · 1.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from datetime import date, timedelta
d = date(2018, 12, 25)
print(d)
# today's date
d = date.today()
print(d)
# date from timestamp
import time
t = time.time()
print(t)
d = date.fromtimestamp(t)
print(d)
d = date.fromtimestamp(1537261418)
print(d)
# date from ordinal
d = date.fromordinal(366)
print(d)
# date from ISO string format, added in Python 3.7
d = date.fromisoformat('2018-09-19')
print(d)
# date class attributes
print(date.min)
print(date.max)
print(date.resolution)
# instance attributes, read only
d = date.today()
print(d.year)
print(d.month)
print(d.day)
# date operations with timedelta
date_tomorrow = date.today() + timedelta(days=1)
print(date_tomorrow)
date_yesterday = date.today() - timedelta(days=1)
print(date_yesterday)
td = date_tomorrow - date_yesterday
print(td)
print(date_tomorrow > date_yesterday)
# instance methods
today = date.today()
print(today)
new_date = today.replace(year=2020)
print(new_date)
print(today.timetuple())
print(today.toordinal())
print(today.weekday())
print(today.isoweekday())
print(today.isocalendar())
print(today.isoformat())
print(today.ctime())
# date to string formatting
print(today.strftime('%Y/%m/%d'))
# string to date instance
from datetime import datetime
dt = datetime.strptime('2018/09/18', '%Y/%m/%d').date()
print(type(dt))
print(dt)