timetuple() Function Of Datetime.date Class In Python
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 :
Index Attributes Values 0 tm_year(Year) any year 1 tm_mon(Month) 1 to 12 2 tm_mday(Day) 1 to 31 3 tm_hour(Hour) 0 to 23 4 tm_min(Minute) 0 to 59 5 tm_sec(Second) 0 to 61 6 tm_wday(Day of the week) 0 to 6 7 tm_yday(Day of Year) 1 to 366 8 tm_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.
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.
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)