|
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.""" |
2 | 3 |
|
3 | | -# Date Class |
| 4 | +"""Date Class""" |
4 | 5 | # Example 1: Creating a Date Object |
5 | 6 | from datetime import date |
6 | 7 | d = date(1996, 12, 11) |
|
9 | 10 | # Example 2: Get Current Date |
10 | 11 | from datetime import date |
11 | 12 | t = date.today() |
12 | | -print(t) |
| 13 | +print(f"Current date: {t}") |
13 | 14 |
|
14 | 15 | # Example 3: Accessing Year, Month and Day Attributes |
15 | 16 | from datetime import date |
16 | 17 | 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}") |
20 | 19 |
|
21 | 20 | # Example 4: Create Date from Timestamp |
22 | 21 | from datetime import datetime |
|
32 | 31 | print(type(date_str)) |
33 | 32 |
|
34 | 33 |
|
35 | | -# Time class |
| 34 | +"""Time class""" |
36 | 35 | # Example 1: Time object representing time in Python |
37 | 36 | from datetime import time |
38 | 37 |
|
|
48 | 47 | my_time = time() |
49 | 48 | print("Time without argument:", my_time) |
50 | 49 |
|
51 | | -# time(hour=26) → ValueError: hour must be in 0..23 |
52 | | -# time(hour='23') → TypeError: string passed instead of int |
53 | | - |
54 | 50 |
|
55 | 51 | # Example 2: Get hours, minutes, seconds and microseconds |
56 | 52 | from datetime import time |
|
67 | 63 |
|
68 | 64 | # Creating Time object |
69 | 65 | Time = time(12,24,36,1212) |
| 66 | +print("Time Object:", Time) |
70 | 67 |
|
71 | 68 | # Converting Time object to string |
72 | 69 | Str = Time.isoformat() |
73 | 70 | print("String Representation:", Str) |
74 | 71 | print(type(Str)) |
75 | 72 |
|
76 | 73 |
|
77 | | - |
78 | | -# Datetime class |
| 74 | +"""Datetime class""" |
79 | 75 | # Example 1: DateTime object representing DateTime in Python |
80 | 76 | from datetime import datetime |
81 | 77 |
|
82 | 78 | # Initializing constructor |
83 | 79 | a = datetime(1999, 12, 12) |
84 | | -print(a) |
| 80 | +print(f"DateTime object: {a}") |
85 | 81 |
|
86 | 82 | # Initializing constructor with time parameters as well |
87 | 83 | a = datetime(1999, 12, 12, 12, 12, 12, 342380) |
88 | | -print(a) |
| 84 | +print(f"DateTime object with time: {a}") |
89 | 85 |
|
90 | 86 |
|
91 | 87 | # Example 2: Get year, month, hour, minute and timestamp |
|
107 | 103 | print("Current date and time is", today) |
108 | 104 |
|
109 | 105 |
|
110 | | - |
111 | 106 | # Example 4: Convert Python Datetime to String |
112 | 107 | from datetime import datetime as dt |
113 | 108 |
|
|
118 | 113 | print(type(string)) |
119 | 114 |
|
120 | 115 |
|
121 | | -# Timezone class |
| 116 | +"""Timezone class""" |
122 | 117 | from datetime import datetime |
123 | 118 | from pytz import timezone |
124 | 119 |
|
|
0 commit comments