ADVERTISEMENT
ADVERTISEMENT

Python datetime timetuple() Method with Example

Python datetime.timetuple() Method: Here, we are going to learn about the timetuple() method of datetime class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 02, 2020

Python datetime.timetuple() Method

datetime.timetuple() method is used to manipulate objects of datetime class of module datetime.

It is an instance method which means that it works on an instance of the class. It returns a time.struct_time which is an object with a named tuple interface containing nine elements.

Following values are present in time.struct_time object:

IndexAttributeValue
0tm_year(for example, 1993)
1tm_monrange [1, 12]
2tm_mdayrange [1, 31]
3tm_hourrange [0, 23]
4tm_minrange [0, 59]
5tm_secrange [0, 61]
6tm_wdayrange [0, 6], Monday is 0
7tm_ydayrange [1, 366]
8tm_isdst0, 1 or -1; see below
N/Atm_zoneabbreviation of timezone name
N/Atm_gmtoffoffset east of UTC in seconds

A datetime timetuple is equivalent to,

    time.struct_time((d.year, d.month, d.day, d.hour, d.minute, d.second, d.weekday(), yday, dst))

Module:

    import datetime

Class:

    from datetime import datetime

Syntax:

    timetuple()

Parameter(s):

  • None

Return value:

The return type of this method is a time.struct_time object which contains date and time information.

Example:

## Python program explaining the 
## use of datetime timetuple() method

from datetime import datetime

## Creating an instance
x = datetime(2020, 4, 29, 10, 50, 40)
print("Current date is:", x)

d = x.timetuple()
print("The tuple of the datetime object", d)
print()

print("We can also access individual elements of this tuple")
for i in d:
    print(i)
print()

x = datetime.now()
print("The tuple of the datetime object:", x.timetuple())

Output

Current date is: 2020-04-29 10:50:40
The tuple of the datetime object time.struct_time(tm_year=2020, tm_mon=4, tm_mday=29, tm_hour=10, tm_min=50, tm_sec=40, tm_wday=2, tm_yday=120, tm_isdst=-1)

We can also access individual elements of this tuple
2020
4
29
10
50
40
2
120
-1

The tuple of the datetime object: time.struct_time(tm_year=2020, tm_mon=5, tm_mday=2, tm_hour=6, tm_min=22, tm_sec=38, tm_wday=5, tm_yday=123, tm_isdst=-1)
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.