Skip to content

Commit fd32275

Browse files
committed
updated datetime module
1 parent 63a4812 commit fd32275

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

41_Datetime_module.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# 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.
1+
"""In Python, date and time are not built-in types but are handled using built-in datetime module.
2+
This module offers classes to efficiently work with dates, times and intervals, providing many useful methods."""
23

3-
# Date Class
4+
"""Date Class"""
45
# Example 1: Creating a Date Object
56
from datetime import date
67
d = date(1996, 12, 11)
@@ -9,14 +10,12 @@
910
# Example 2: Get Current Date
1011
from datetime import date
1112
t = date.today()
12-
print(t)
13+
print(f"Current date: {t}")
1314

1415
# Example 3: Accessing Year, Month and Day Attributes
1516
from datetime import date
1617
t = date.today()
17-
print(t.year)
18-
print(t.month)
19-
print(t.day)
18+
print(f"Year: {t.year}, Month: {t.month}, Day: {t.day}")
2019

2120
# Example 4: Create Date from Timestamp
2221
from datetime import datetime
@@ -32,7 +31,7 @@
3231
print(type(date_str))
3332

3433

35-
# Time class
34+
"""Time class"""
3635
# Example 1: Time object representing time in Python
3736
from datetime import time
3837

@@ -48,9 +47,6 @@
4847
my_time = time()
4948
print("Time without argument:", my_time)
5049

51-
# time(hour=26) → ValueError: hour must be in 0..23
52-
# time(hour='23') → TypeError: string passed instead of int
53-
5450

5551
# Example 2: Get hours, minutes, seconds and microseconds
5652
from datetime import time
@@ -67,25 +63,25 @@
6763

6864
# Creating Time object
6965
Time = time(12,24,36,1212)
66+
print("Time Object:", Time)
7067

7168
# Converting Time object to string
7269
Str = Time.isoformat()
7370
print("String Representation:", Str)
7471
print(type(Str))
7572

7673

77-
78-
# Datetime class
74+
"""Datetime class"""
7975
# Example 1: DateTime object representing DateTime in Python
8076
from datetime import datetime
8177

8278
# Initializing constructor
8379
a = datetime(1999, 12, 12)
84-
print(a)
80+
print(f"DateTime object: {a}")
8581

8682
# Initializing constructor with time parameters as well
8783
a = datetime(1999, 12, 12, 12, 12, 12, 342380)
88-
print(a)
84+
print(f"DateTime object with time: {a}")
8985

9086

9187
# Example 2: Get year, month, hour, minute and timestamp
@@ -107,7 +103,6 @@
107103
print("Current date and time is", today)
108104

109105

110-
111106
# Example 4: Convert Python Datetime to String
112107
from datetime import datetime as dt
113108

@@ -118,7 +113,7 @@
118113
print(type(string))
119114

120115

121-
# Timezone class
116+
"""Timezone class"""
122117
from datetime import datetime
123118
from pytz import timezone
124119

0 commit comments

Comments
 (0)