timetuple(): named tuple containing components of the date and time

timetuple : Details about date and time as elements of a tuple.

The timetuple() method in Python's datetime module returns a named tuple, time.struct_time, containing various components of a date and time object. This method is particularly useful for extracting detailed information from date and datetime objects.

Components of time.struct_time

The time.struct_time tuple includes the following attributes:

  • tm_year: Year (e.g., 2025)
  • tm_mon: Month (1-12)
  • tm_mday: Day of the month (1-31)
  • tm_hour: Hour (0-23)
  • tm_min: Minute (0-59)
  • tm_sec: Second (0-59)
  • tm_wday: Weekday (0=Monday, 6=Sunday)
  • tm_yday: Day of the year (1-366)
  • tm_isdst: Daylight Saving Time flag (-1, 0, 1)

tm_isdst :Daylight Saving Time as flag. It is greater than 0 if DST is in effect. 0 if DST is not in effect and less than 0 if no information is available.

Today's details as timetuple

from datetime import date

dt = date.today().timetuple()
print(dt)
Output
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=16, tm_hour=0,
 tm_min=0, tm_sec=0, tm_wday=0, tm_yday=259, tm_isdst=-1)
Getting time details along with date
from datetime import datetime

dt = datetime.now().timetuple()
print(dt)
Output is here
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=17, tm_hour=6, 
tm_min=18, tm_sec=26, tm_wday=1, tm_yday=260, tm_isdst=-1)

Using date object

import datetime

dt = datetime.datetime(2019, 12, 31)
print(dt.timetuple())
Output
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=31, tm_hour=0, 
tm_min=0, tm_sec=0, tm_wday=1, tm_yday=365, tm_isdst=-1)
Using datetime
import datetime

# Year, Month, Date, Hour, Minute, Second, Microsecond
dt = datetime.datetime(2019, 12, 31, 23, 59, 59, 345234)
print(dt.timetuple())
Output
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=31, tm_hour=23, 
tm_min=59, tm_sec=59, tm_wday=1, tm_yday=365, tm_isdst=-1)

As we get a tuple we can apply all methods of tuple.
Total Number of elements in output is here ( we will use len())
print(len(dt.timetuple()))
output
9
Value of 2nd element i.e date part of the object. ( first element position is 0 )
print(dt.timetuple()[2])
Output is
31
Displaying all values by looping.
for i in dt.timetuple():
    print(i)
Output is here
2019
12
31
23
59
59
1
365
-1

Example 1: Extracting Components from a datetime Object

Let's create a datetime object and extract its components using the timetuple() method:

from datetime import datetime

dt = datetime(2025, 2, 8, 14, 30, 45)
tt = dt.timetuple()

print(f"Year: {tt.tm_year}")
print(f"Month: {tt.tm_mon}")
print(f"Day: {tt.tm_mday}")
print(f"Hour: {tt.tm_hour}")
print(f"Minute: {tt.tm_min}")
print(f"Second: {tt.tm_sec}")
print(f"Weekday: {tt.tm_wday}")
print(f"Day of the year: {tt.tm_yday}")
print(f"DST flag: {tt.tm_isdst}")

Example 2: Converting datetime to Timestamp

We can convert a datetime object to a timestamp using the timetuple() method in conjunction with the time module:

from datetime import datetime
import time

dt = datetime(2025, 2, 8, 14, 30, 45)
timestamp = time.mktime(dt.timetuple())

print(f"Timestamp: {timestamp}")

Example 3: Formatting datetime Using strftime()

The timetuple() method can be used with strftime() to format datetime objects into readable strings:

from datetime import datetime

dt = datetime(2025, 2, 8, 14, 30, 45)
formatted_date = dt.strftime("%A, %B %d, %Y %I:%M:%S %p")

print(f"Formatted Date: {formatted_date}")

Use Cases of timetuple()

The timetuple() method is beneficial in scenarios such as:

  • Interfacing with APIs or systems that require time in tuple format.
  • Performing detailed date and time analysis by accessing individual components.
  • Converting datetime objects to timestamps for storage or comparison purposes.

By leveraging the timetuple() method, we can efficiently dissect and utilize the components of date and time objects in Python, enhancing our ability to perform precise time-based computations and formatting.


All Date Objects Date Timedelta
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer