How to print complete TimeTuple in Python?

In Python, a TimeTuple is a named tuple containing 9 elements that represent different components of date and time. The timetuple() method converts datetime objects into this structured format, making it easy to access individual time components like year, month, day, hour, minute, and second.

The time tuple contains 9 integers: year, month, day, hour, minute, second, weekday, year day, and daylight saving time flag. Here are different ways to print a complete time tuple ?

Using datetime.date.today() Method

The datetime.date.today() method returns the current local date. When converted to a time tuple, it includes date information with time components set to zero ?

import datetime

# Get the current date
today = datetime.date.today()

# Print the complete time tuple
print("Complete Time Tuple:", today.timetuple())

# Access individual components
time_tuple = today.timetuple()
print("Year:", time_tuple.tm_year)
print("Month:", time_tuple.tm_mon)
print("Day:", time_tuple.tm_mday)
Complete Time Tuple: time.struct_time(tm_year=2024, tm_mon=1, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=15, tm_isdst=-1)
Year: 2024
Month: 1
Day: 15

Using datetime.datetime.now() Method

The datetime.datetime.now() method returns the current local date and time. This provides a complete time tuple with all components populated ?

import datetime

# Get the current date and time
now = datetime.datetime.now()

# Print the complete time tuple
print("Complete Time Tuple:", now.timetuple())

# Access time components
time_tuple = now.timetuple()
print("Hour:", time_tuple.tm_hour)
print("Minute:", time_tuple.tm_min)
print("Second:", time_tuple.tm_sec)
Complete Time Tuple: time.struct_time(tm_year=2024, tm_mon=1, tm_mday=15, tm_hour=14, tm_min=30, tm_sec=45, tm_wday=0, tm_yday=15, tm_isdst=-1)
Hour: 14
Minute: 30
Second: 45

Using a Specific Date

You can create a datetime object for any specific date and convert it to a time tuple ?

import datetime

# Create a datetime object for a specific date
specific_date = datetime.datetime(2023, 12, 25, 18, 30, 0)

# Print the complete time tuple
print("Complete Time Tuple:", specific_date.timetuple())

# Understanding tuple components
time_tuple = specific_date.timetuple()
print("Weekday (0=Monday):", time_tuple.tm_wday)
print("Day of year:", time_tuple.tm_yday)
print("DST flag:", time_tuple.tm_isdst)
Complete Time Tuple: time.struct_time(tm_year=2023, tm_mon=12, tm_mday=25, tm_hour=18, tm_min=30, tm_sec=0, tm_wday=0, tm_yday=359, tm_isdst=-1)
Weekday (0=Monday): 0
Day of year: 359
DST flag: -1

TimeTuple Components

Attribute Description Range
tm_year Year 4-digit year
tm_mon Month 1-12
tm_mday Day of month 1-31
tm_hour Hour 0-23
tm_min Minute 0-59
tm_sec Second 0-59
tm_wday Weekday 0-6 (Monday=0)
tm_yday Day of year 1-366
tm_isdst Daylight saving time -1, 0, or 1

Conclusion

The timetuple() method provides a structured way to access datetime components. Use datetime.now().timetuple() for current time or create specific datetime objects for custom dates. This approach is particularly useful for time calculations and formatting operations.

---
Updated on: 2026-03-24T19:09:41+05:30

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements