Prerequisites: Python datetime module with examples
In Python, the datetime module is used to operate with date and time objects. This module has different functions and classes for working with date and time parsing, formatting and arithmetic operations. The datetime module has various constants such as
MAXYEAR etc, various classes like
datetime.datetime etc and various instance methods such as
isocalendar() etc of date object and
timestamp(), etc. of time object.
In order to use the
timetuple() method we need to import the datetime module. The timetuple() method is an instance method for the datetime module this method returns
time.struct_time object. The attributes of the
time.struct_time object can be accessed by index or name of the attribute.
struct_time object has attributes for representing both date and time fields, these attributes are stored in tuples:
|0||4 Digit Year||tm_year||Example: 2020|
|1||Month||tm_mon||1 to 12|
|2||Day||tm_mday||1 to 31|
|3||Hour||tm_hour||0 to 23|
|4||Minute||tm_min||0 to 59|
|5||Second||tm_sec||0 to 61|
|6||Day of Week||tm_wday||0 to 6|
|7||Day of Year||tm_yday||1 to 366|
|8||Daylight Saving Time||tm_isdst||-1, 0, 1|
tm_isdst attribute is a flag which is set to 0 when daylight saving time active is off, 1 if daylight saving time is active and -1 if daylight saving time is determined by the compiler, the
tm_yday attribute is based on Julian Day Number and in
tm_sec, values 60 or 61 are leap seconds.
Example 1: Below example shows the time tuple of the present date.
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=29, tm_hour=14, tm_min=13, tm_sec=32, tm_wday=2, tm_yday=29, tm_isdst=-1)
In the above program, the
obj is assigned with the present date and then
timetuple() method is used to obtain the attributes of
obj in tuples and is displayed. We can also use a loop to display the attributes of
Example 2: Let us look at another example where we use a custom date.
Year: 1999 Month: 4 Day: 6 Hour: 0 Minute: 0 Second: 0 Day of Week: 1 Day of Year: 96 Daylight Saving Time: -1
birthDate is assigned with a custom date and then each attribute of that object is displayed by the index of the tuple.The parameters retrieved from the
time.struct_time structure can be accessed as elements of tuple and the year, month, and day fields assigned as the datetime object(here 1999, 4 and 6) and fields referring to the hour, minutes, seconds are set to zero.
- Python | Sort tuple list by Nth element of tuple
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python | Replace tuple according to Nth tuple element
- Python program to find difference between current time and given time
- Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
- Python | Tuple multiplication
- Python - Sum of tuple elements
- Python | Zip Uneven Tuple
- Tuple Division in Python
- Python | Check if tuple has any None value
- Python - Test if Tuple contains K
- Python | Reversing a Tuple
- Python - Clearing a tuple
- Python | Tuple XOR operation