Datetime.timetuple() in Python

Method Name:

timetuple

Method Signature:

timetuple()

Method Overview:

  • The timetuple() method returns a named tuple of type time.struct_time.

 

  • Since a datetime instance has both date related attributes and time related attributes all the below attributes of the returned object time.struct_time will be set. The attributes are,
    • tm_year
    • tm_mon
    • tm_mday
    • tm_hour
    • tm_min
    • tm_sec
    • tm_wday
    • tm_yday
    • tm_isdst

       

  • The tm_isdst is set based on the tzinfo attribute of the datetime.

If the tzinfo has the daylight saving set tm_isdst will be set accordingly as 0 or 1. Else, -1 will be set for tm_isdst.

 

  • Note that attributes of the returned structure can be accessed like the elements of a tuple or like attributes of an object.

 

Example:

#import the datetime module

import datetime

 

dateToday = datetime.datetime.today()

 

#Get the attributes of the datetime instance as a tuple

attributesInTuple = dateToday.timetuple()

 

# print the tuple elements using a for loop

for attribute in attributesInTuple:

    print(attribute)

 

 

Output:

2017

2

15

14

49

26

2

46

-1

 

 


Copyright 2024 © pythontic.com