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 - Convert Tuple String to Integer Tuple
- Python - Raise elements of tuple as power to another tuple
- Python | Sort tuple list by Nth element of 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()...)
- Unpacking a Tuple in Python
- Python | Add dictionary to tuple
- Python | Zip Uneven Tuple
- Python | Tuple XOR operation
- Python - Test if Tuple contains K
- Python - Clearing a tuple
- Python - Sum of tuple elements
- Python | Reversing a Tuple
- Python | Check for None Tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.