Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

timetuple() Function Of Datetime.date Class In Python

  • Last Updated : 23 Aug, 2021

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 :

IndexAttributesValues
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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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.



Python3




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

Output:

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 :

2021

8

2

18

32



56

0

214

-1

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.

Python3




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
print(xmasTimeTuple)

Output:

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
Recommended Articles
Page :