Python Datetime.timetuple() Method
-
Syntax of Python
datetime.timetuple()
Method -
Example Codes: Working With the
datetime.timetuple()
Method -
Example Codes: Enter a DateTime Object in the
datetime.timetuple()
Method -
Example Codes: Access Elements Using a Loop in the
datetime.timetuple()
Method -
Example Codes: Use the Subscript Notation With the
datetime.timetuple()
Method
Python datetime.timetuple()
method is an efficient way of finding attributes of DateTime representing both date and time fields. It is a named tuple interface having nine elements.
Syntax of Python datetime.timetuple()
Method
datetime.timetuple()
Parameters
No parameters are required.
Return
The return type of this method is an object of type time.struct_time
. The following attributes are in the returned object:
index | attribute name | value |
---|---|---|
0 | tm_year |
valid year e.g, 1993 |
1 | tm_mon |
[1, 12] |
2 | tm_mday |
[1, 31] |
3 | tm_hour |
[0, 23] |
4 | tm_min |
[0, 59] |
5 | tm_sec |
[0, 61] |
6 | tm_wday |
[0, 6], Monday=0 |
7 | tm_yday |
[1, 366] |
8 | tm_isdst |
0, 1, or -1 |
Example Codes: Working With the datetime.timetuple()
Method
import datetime
datetime_object = datetime.datetime.today()
attributes = datetime_object.timetuple()
print(attributes)
Output:
time.struct_time(tm_year=2022, tm_mon=9, tm_mday=3, tm_hour=11, tm_min=5, tm_sec=16, tm_wday=5, tm_yday=246, tm_isdst=-1)
The last attribute represents the daylight saving setting. The tm_isdst
will be either 0 or 1 based on the daylight saving setting.
Otherwise, -1 will be set for tm_isdst
.
Example Codes: Enter a DateTime Object in the datetime.timetuple()
Method
import datetime
datetime_object = datetime.datetime(2021, 4, 29, 16, 50, 40)
print("The random date is: ", datetime_object)
attribute = datetime_object.timetuple()
print("The tuple of the datetime object is ", attribute)
Output:
The random date is: 2021-04-29 16:50:40
The tuple of the datetime object is time.struct_time(tm_year=2021, tm_mon=4, tm_mday=29, tm_hour=16, tm_min=50, tm_sec=40, tm_wday=3, tm_yday=119, tm_isdst=-1)
Any valid DateTime object can be represented as a tuple.
Example Codes: Access Elements Using a Loop in the datetime.timetuple()
Method
import datetime
datetime_object = datetime.datetime.now()
print("Today's date is: ", datetime_object)
attribute = datetime_object.timetuple()
for i in attribute:
print(i)
Output:
Today's date is: 2022-09-03 11:13:09.779883
2022
9
3
11
13
9
5
246
-1
The above code can easily be used to get all the elements separately.
Example Codes: Use the Subscript Notation With the datetime.timetuple()
Method
from datetime import datetime
datetime_object = datetime.now()
attributes = datetime_object.timetuple()
print("Year: ", attributes[0])
print("Hour: ", attributes[3])
print("Day of the year:", attributes[7])
Output:
Year: 2022
Hour: 11
Day of the year: 246
The subscript notation can retrieve any element of the object separately.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn