ADVERTISEMENT
ADVERTISEMENT

Python date timetuple() Method with Example

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

Python date.timetuple() Method

date.timetuple() method is used to manipulate objects of date 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:

Index Attribute Value
0 tm_year (for example, 1993)
1 tm_mon range [1, 12]
2 tm_mday range [1, 31]
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]
6 tm_wday range [0, 6], Monday is 0
7 tm_yday range [1, 366]
8 tm_isdst 0, 1 or -1; see below
N/A tm_zone abbreviation of timezone name
N/A tm_gmtoff offset east of UTC in seconds

A date timetuple is equivalent to,

    time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))

Since it is a date object, time values are missing because of which those attributes are set to zero(index- 3,4,5). As date object is naive, the timezone information are also missing.

Module:

    import datetime

Class:

    from datetime import date

Syntax:

    timetuple()

Parameter(s):

  • None

Return value:

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

Example:

## Python program explaining the 
## use of date class instance methods

from datetime import date

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

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

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

Output

Current date is: 2020-04-29

The tuple of the date object time.struct_time(tm_year=2020, tm_mon=4, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=120, tm_isdst=-1)

We can also access individual elements of this tuple
2020
4
29
0
0
0
2
120
-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.