-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_time_example.py
More file actions
58 lines (47 loc) · 1.5 KB
/
date_time_example.py
File metadata and controls
58 lines (47 loc) · 1.5 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
#DateTime
#Working with dates, times, deltas and formats
#Imports
from datetime import datetime, timedelta
from time import strftime
def main():
#Now
#If the host machine is in the UTC timezone then there will be no difference between
now = datetime.now()
utc = datetime.utcnow()
print(f'Now: {now}')
print(f'UTC: {utc}')
print(f'Offset: {now.utcoffset()}')
#Time
print(f'Hour: {now.hour}')
print(f'Minute: {now.minute}')
print(f'Second: {now.second}')
print(f'Micro: {now.microsecond}')
#Date
print(f'Year: {now.year}')
print(f'Month: {now.month}')
print(f'Day: {now.day}')
#Timedelta
print(f'Next Month: {now + timedelta(days=30)}')
print(f'Last Week: {now + timedelta(weeks=-1)}')
print(f'5 Hours: {now + timedelta(hours=5)}')
print(f'45 Seconds: {now + timedelta(seconds=45)}')
print(f'200 Milliseconds: {now + timedelta(milliseconds=200)}')
print(f'10 Microseconds: {now + timedelta(microseconds=10)}')
#ISO Strings
d = datetime.fromisoformat('2020-12-16')
print(d)
try:
m = datetime.fromisoformat('20:26-06:00')
except Exception as ex:
print(ex.args)
print(f'ISO: {now.isoformat()}')
#Formatting
#https://www.w3schools.com/python/python_datetime.asp
print(now.strftime('%y'))
print(now.strftime('%Y'))
print(now.strftime('%d'))
print(now.strftime('%D'))
print(now.strftime('%b'))
print(now.strftime('Today is %B %d'))
if __name__ == "__main__":
main()