-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path41_Datetime_module.py
More file actions
127 lines (93 loc) · 3.11 KB
/
41_Datetime_module.py
File metadata and controls
127 lines (93 loc) · 3.11 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
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
"""In Python, date and time are not built-in types but are handled using built-in datetime module.
This module offers classes to efficiently work with dates, times and intervals, providing many useful methods."""
"""Date Class"""
# Example 1: Creating a Date Object
from datetime import date
d = date(1996, 12, 11)
print(d)
# Example 2: Get Current Date
from datetime import date
t = date.today()
print(f"Current date: {t}")
# Example 3: Accessing Year, Month and Day Attributes
from datetime import date
t = date.today()
print(f"Year: {t.year}, Month: {t.month}, Day: {t.day}")
# Example 4: Create Date from Timestamp
from datetime import datetime
date_time = datetime.fromtimestamp(1887639468)
print(date_time)
print(date_time.date())
# Example 5: Convert Date to String
from datetime import date
t = date.today()
date_str = t.isoformat()
print(date_str)
print(type(date_str))
"""Time class"""
# Example 1: Time object representing time in Python
from datetime import time
# Create time object with hour, minute and second
my_time = time(13, 24, 56)
print("Entered time:", my_time)
# Time object with only minute specified
my_time = time(minute=12)
print("Time with one argument:", my_time)
# Time object with default (00:00:00)
my_time = time()
print("Time without argument:", my_time)
# Example 2: Get hours, minutes, seconds and microseconds
from datetime import time
Time = time(11, 34, 56)
print("hour =", Time.hour)
print("minute =", Time.minute)
print("second =", Time.second)
print("microsecond =", Time.microsecond)
# Example 3: Convert Time object to String
from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
print("Time Object:", Time)
# Converting Time object to string
Str = Time.isoformat()
print("String Representation:", Str)
print(type(Str))
"""Datetime class"""
# Example 1: DateTime object representing DateTime in Python
from datetime import datetime
# Initializing constructor
a = datetime(1999, 12, 12)
print(f"DateTime object: {a}")
# Initializing constructor with time parameters as well
a = datetime(1999, 12, 12, 12, 12, 12, 342380)
print(f"DateTime object with time: {a}")
# Example 2: Get year, month, hour, minute and timestamp
from datetime import datetime
a = datetime(1999, 12, 12, 12, 12, 12)
print("year =", a.year)
print("month =", a.month)
print("hour =", a.hour)
print("minute =", a.minute)
print("timestamp =", a.timestamp())
# Example 3: Current date and time
from datetime import datetime
# Calling now() function
today = datetime.now()
print("Current date and time is", today)
# Example 4: Convert Python Datetime to String
from datetime import datetime as dt
# Getting current date and time
now = dt.now()
string = dt.isoformat(now)
print(string)
print(type(string))
"""Timezone class"""
from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
now_utc = datetime.now(timezone('UTC')) # Current time in UTC
print(now_utc.strftime(format))
timezones = ['Asia/Kolkata', 'Europe/Kiev', 'America/New_York']
for tzone in timezones:
now_asia = now_utc.astimezone(timezone(tzone)) # Convert to Asia/Kolkata time zone
print(now_asia.strftime(format))