-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatetime.py
134 lines (96 loc) · 3.21 KB
/
Datetime.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# datetime is a module
import datetime
print(dir(datetime))
print(dir(datetime.datetime))
# Current date and time
print(datetime.datetime.now())
print('*'*30)
# Current date
print(datetime.datetime.date(datetime.datetime.now()))
print(datetime.datetime.now().date())
print('*'*30)
# Current time
print(datetime.datetime.time(datetime.datetime.now()))
print(datetime.datetime.now().time())
print('*'*30)
# Current Hour
print(datetime.datetime.now().hour)
print('*'*30)
# Current Minute
print(datetime.datetime.now().minute)
print('*'*30)
# Current Second
print(datetime.datetime.now().second)
print('*'*30)
# Current Year
print(datetime.datetime.now().year)
print('*'*30)
# Current month
print(datetime.datetime.now().month)
print('*'*30)
# Current Day
print(datetime.datetime.now().day)
print('*'*30)
# Strat and End of Date
print(datetime.datetime.min)
print(datetime.datetime.max)
print('*'*30)
# Strat and End of Time
print(datetime.time.min)
print(datetime.time.max)
print('*'*30)
# Print Specific Date
print(datetime.datetime(2021, 6, 1))
print(datetime.datetime(2021, 6, 1,10,30,45,25541))
print('*'*30)
# Subtract Dates
MyBirthday = datetime.datetime(2000,12,16)
CurrentDate = datetime.datetime.now()
print(f"I Lived For : {CurrentDate - MyBirthday}")
print(f"I Lived For : {(CurrentDate - MyBirthday).days} Days")
print('*'*30)
########################## Date Formatting ##########################
# https://strftime.org/
# strftime -> String Format Time
# %Y -> Year
# %m -> Month
# %d -> Day
print(dir(datetime.datetime))
print('*'*30)
# Format Date
Birthday = datetime.datetime(2000,12,16)
print(Birthday.strftime("%Y/%m/%d")) # 2000/12/16
print(Birthday.strftime("%a")) # Sat
print(Birthday.strftime("%A")) # Saturday
print(Birthday.strftime("%m")) # 12
print(Birthday.strftime("%b")) # Dec
print(Birthday.strftime("%B")) # December
print(Birthday.strftime("%y")) # 00
print(Birthday.strftime("%Y")) # 2000
print(Birthday.strftime("%d %B %Y")) # 16 December 2000
print(Birthday.strftime("%d-%b-%Y")) # 16-Dec-2000
print(Birthday.strftime("%d-%m-%Y")) # 16-12-2000
print("*"*30)
def get_n_days_after_date(dateFormat = "%d %m %Y", add_dayes = 120):
date_n_days_after = datetime.datetime.now() + datetime.timedelta(days = add_dayes)
return date_n_days_after.strftime(dateFormat)
print(get_n_days_after_date(add_dayes= 100))
# to find the amount of time between dates or determine what the date will be tomorrow. This can be accomplished using timedelta objects
today = datetime.date.today()
print('Today:', today)
yesterday = today - datetime.timedelta(days=1)
print('Yesterday:', yesterday)
tomorrow = today + datetime.timedelta(days=1)
print('Tomorrow:', tomorrow)
print('Time between tomorrow and yesterday:', tomorrow - yesterday)
# Fuzzy datetime parsing (extracting datetime out of a text)
# it is possible to extract a date out of a text using the dateutil parser in a "fuzzy" mode
from dateutil.parser import parse
dateTime = parse("Today is January 1, 2047 at 8:21:00AM", fuzzy=True)
print(dateTime)
# Iterate Over Dates
day_delta = datetime.timedelta(days=1)
start_date = datetime.datetime.now()
end_date = start_date + 7 * day_delta
for date in range((end_date - start_date).days):
print(start_date + date * day_delta)