Skip to content
Related Articles
Open in App
Not now

Related Articles

timetuple() Function Of Class In Python

Improve Article
Save Article
  • Last Updated : 23 Aug, 2021
Improve Article
Save Article

timetuple() method returns a time.struct time object which is a named tuple. A named tuple object has attributes that may be accessed by an index or a name. The struct time object has properties for both the date and time fields, as well as a flag that specifies whether or not Daylight Saving Time is in effect. The year, month, and day fields of the named tuple provided by the timetuple() method will be set according to the date object, but the hour, minutes, and seconds values will be set to zero. The DST flag on the returned object will be set to -1. The characteristics of the time.struct time object may be accessed by index or attribute name. The struct time object contains attributes for expressing both date and time fields, which are kept in tuples :

0tm_year(Year) any year
1tm_mon(Month)1 to 12
2tm_mday(Day)1 to 31
3tm_hour(Hour)0 to 23
4tm_min(Minute) 0 to 59
5tm_sec(Second) 0 to 61
6tm_wday(Day of the week)0 to 6
7tm_yday(Day of Year)1 to 366
8tm_isdst(Daylight Saving Time)-1, 0, 1

Example 1: timetuple() with current date.

Here we are going demonstrate timetuple() of the current date. The current date is allocated to the DateTime object, and the timetuple() function is used to retrieve the properties of the object in tuples, which are then shown. A loop may also be used to display the characteristics of object.


import datetime
# creating current date object
currentDate =
# datetime instance's attributes
# are returned as a tuple.
currentDateTimeTuple = currentDate.timetuple()
# showing the object's tuples
print("Using a for loop, output the tuple values :")
for value in currentDateTimeTuple:


time.struct_time(tm_year=2021, tm_mon=8, tm_mday=2, tm_hour=18, tm_min=32, tm_sec=56, tm_wday=0, tm_yday=214, tm_isdst=-1)

Using a for loop, output the tuple values :










Example 2: timetuple() with a specific date.

Consider another scenario in which we utilize a Christmas date. The DateTime object Christmas is created, and then the index of the tuple displays each property of that object.


import datetime
# creating xmas date object
xmas = datetime.datetime(2021, 12, 25)
# datetime instance's attributes
# are returned as a tuple.
xmasTimeTuple = xmas.timetuple()
# showing the object's tuples


time.struct_time(tm_year=2021, tm_mon=12, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=359, tm_isdst=-1)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!