In Python, we need the datetime module to work with time and date objects. The datetime module consists of different classes and functions to operate with date and time.
Datetime involves date and time formatting, parsing, and arithmetic operators. The module also contains multiple constants like MAXYEAR
and MINYEAR
. Different instance functions and classes are also involved regarding date and time.
timetuple
methodWe need to import the datetime module to use the timetuple()
method. This method’s parameters can be accessed by the name and index of parameters. This object contains parameters for both date and time that are stored in tuples.
index | attibute | value |
0 | tm_year | (e-g, 1999) |
1 | tm_mon | range [1, 12] |
2 | tm_mday | range [1, 31] |
3 | tm_hour | [0, 23] |
4 | tm_min | [0, 59] |
5 | tm_sec | range [0, 61] |
6 | tm_wday | range [0, 6], 0 represents Monday |
7 | tm_yday | range [1, 366] |
8 | tm_isdst | 0,1 or -1 |
N.A | tm_zone | Name of time zone |
timetuple()
The timetuple()
method does not take any parameters.
timetuple()
returns the time.struct_time()
object as a tuple that contains the date and time information, i.e., timestamps.
Note: This method is part of the
time
module and thus returns the object oftime
.
The code below demonstrates how the timetuple()
method works.
from datetime import date## Create the instancetoday = date(2019, 5, 12)print("Date:", today)time_tuple = today.timetuple()# print time_tupeprint("\n date object's tuple:\n", time_tuple)# printing elements as tupleprint("\nSpecific elements of this tuple can also be accessed: ")attributes = ['tm_year', 'tm_mon','tm_mday', 'tm_hour','tm_min', 'tm_sec', 'tm_wday', 'tm_yday','tm_isdst']i= 0for t in time_tuple:print(attributes[i], "=",t)i+=1
(2019, 5, 12)
.timetuple()
method to get the time.struct_time
object that contains a tuple of multiple values as shown in the table above.time_tuple
values to the console.time.struct_time
object one by one to the console with their names in parallel.