The Wayback Machine - https://web.archive.org/web/20241128015540/https://www.geeksforgeeks.org/timetuple-function-of-datetime-date-class-in-python/
Open In App

timetuple() Function Of Datetime.date Class In Python

Last Updated : 23 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

timetuple() method returns a time.struct time object which is a named tuple. A named tuple object has attributes that may be accessed by an index or a name. The struct time object has properties for both the date and time fields, as well as a flag that specifies whether or not Daylight Saving Time is in effect. The year, month, and day fields of the named tuple provided by the timetuple() method will be set according to the date object, but the hour, minutes, and seconds values will be set to zero. The DST flag on the returned object will be set to -1. The characteristics of the time.struct time object may be accessed by index or attribute name. The struct time object contains attributes for expressing both date and time fields, which are kept in tuples :

Index Attributes Values
0 tm_year(Year)  any year
1 tm_mon(Month) 1 to 12
2 tm_mday(Day) 1 to 31
3 tm_hour(Hour) 0 to 23
4 tm_min(Minute)  0 to 59
5 tm_sec(Second)  0 to 61
6 tm_wday(Day of the week) 0 to 6
7 tm_yday(Day of Year) 1 to 366
8 tm_isdst(Daylight Saving Time) -1, 0, 1

Example 1: timetuple() with current date.

Here we are going demonstrate timetuple() of the current date. The current date is allocated to the DateTime object, and the timetuple() function is used to retrieve the properties of the object in tuples, which are then shown. A loop may also be used to display the characteristics of object.

Python3




import datetime
  
# creating current date object
currentDate = datetime.datetime.today()
    
# datetime instance's attributes
# are returned as a tuple.
currentDateTimeTuple = currentDate.timetuple()
    
# showing the object's tuples
print(currentDateTimeTuple)
print()
print("Using a for loop, output the tuple values :")
  
for value in currentDateTimeTuple:
    print(value)


Output:

time.struct_time(tm_year=2021, tm_mon=8, tm_mday=2, tm_hour=18, tm_min=32, tm_sec=56, tm_wday=0, tm_yday=214, tm_isdst=-1)

Using a for loop, output the tuple values :

2021

8

2

18

32

56

0

214

-1

Example 2: timetuple() with a specific date.

Consider another scenario in which we utilize a Christmas date. The DateTime object Christmas is created, and then the index of the tuple displays each property of that object.

Python3




import datetime
  
# creating xmas date object
xmas = datetime.datetime(2021, 12, 25)
    
# datetime instance's attributes
# are returned as a tuple.
xmasTimeTuple = xmas.timetuple()
    
# showing the object's tuples
print(xmasTimeTuple)


Output:

time.struct_time(tm_year=2021, tm_mon=12, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=359, tm_isdst=-1)


Previous Article
Next Article

Similar Reads

Python | Pandas Timestamp.timetuple
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.timetuple() function return a time tuple for the given Timestamp object. The returned tuple contains values ranging fro
2 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
fromisoformat() Function Of Datetime.date Class In Python
The fromisoformat() function is used to constructs a date object from a specified string that contains a date in ISO format. i.e., yyyy-mm-dd. Syntax: @classmethod fromisoformat(date_string) Parameters: This function accepts a parameter which is illustrated below: date_string: This is the specified ISO format i.e., yyyy-mm-dd date string whose date
2 min read
isocalendar() Function Of Datetime.date Class In Python
The isocalendar() function is used to return a tuple containing ISO Year, ISO Week Number, and ISO Weekday. Note: According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week.Therefore, ISO years always start with Monday.ISO years can have either 52 full weeks or 53 full weeks.ISO years do not have any fractional weeks
2 min read
fromtimestamp() Function Of Datetime.date Class In Python
The fromtimestamp() function is used to return the date corresponding to a specified timestamp. Note: Here the timestamp is ranging from the year 1970 to the year 2038, and this function does not consider leap seconds if any present in the timestamp. This function is a class method. Syntax: @classmethod fromtimestamp(timestamp) Parameters: This fun
2 min read
ctime() Function Of Datetime.date Class In Python
The ctime() function is used to return a string containing the date and time. Syntax: ctime() Parameters: This function does not accept any parameter. Return values: This function returns a string containing the date and time. The format of the string representation: The string is of 24-character length.Week is printed as a three-letter word.Sun, M
2 min read
Isoformat() Function Of Datetime.date Class In Python
The Isoformat() function is used to return a string of date, time, and UTC offset to the corresponding time zone in ISO 8601 format. The standard ISO 8601 format is all about date formats for the Gregorian calendar. This format prescribes that a calendar date needs to be represented using a 4-digit year followed by a two-digit month and a two-digit
3 min read
weekday() Function Of Datetime.date Class In Python
The weekday() of datetime.date class function is used to return the integer value corresponding to the specified day of the week. Syntax: weekday() Parameters: This function does not accept any parameter. Return values: This function returns the integer mapping corresponding to the specified day of the week. Below table shows the integer values cor
3 min read
replace() Function Of Datetime.date Class In Python
replace() function is used to manipulate the object of DateTime class of module of DateTime. Generally, it replaces the date( Year, Month, Day) and returns a new DateTime object. Syntax: replace(year=self.year, month=self.month, day=self.day) Parameters: Year: New year value (range: 1 <= year <= 9999)month: New month value(range: 1 <= mont
2 min read
Fromordinal() Function Of Datetime.date Class In Python
The fromordinal() function is used to return the Gregorian date corresponding to a specified Gregorian ordinal. This is the opposite of the toordinal() function that is used to convert a Gregorian date to a Gregorian ordinal. When a negative ordinal value or an ordinal beyond the value returned by the date.max.toordinal() is passed to the parameter
2 min read
toordinal() Function Of Datetime.date Class In Python
The toordinal() function is used to return the proleptic Gregorian ordinal of a specified datetime instance. Note: The Proleptic Gregorian ordinal gives the number of days elapsed from the date 01/Jan/0001. And here ordinal is called Proleptic since the Gregorian calendar itself is followed from October 1582. Syntax: toordinal() Parameters: This fu
2 min read
Isoweekday() Function Of Datetime.date Class In Python
isoweekday() is a function that returns an integer that tells the given date falls on. The integer it returns represents a day according to the table given below. Syntax: datetime.isoweekday() Return Value: an integer in range of [1,7] Integer ReturnedDay of the week1Monday2Tuesday3Wednesday4Thursday5Friday6Saturday7Sunday Example 1: This program g
3 min read
Pandas Series dt.date | Extract Date From DateTime Objects
The dt.date attribute extracts the date part of the DateTime objects in a Pandas Series. It returns the NumPy array of Python datetime.date objects, mainly the date part of timestamps without information about the time and timezone. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '201
2 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
Python DateTime - Date Class
The object of the Date class represents the naive date containing year, month, and date according to the current Gregorian calendar. This date can be extended indefinitely in both directions. The January 1 of year 1 is called day 1 and January 2 or year 2 is called day 2 and so on. Syntax: class datetime.date(year, month, day) The arguments must be
4 min read
Manipulate Date and Time with the Datetime Module in Python
Have you ever wondered about working with Date and Time with Python? If you have then you must have noticed that Python does not provide any built-in method to work with either Date or Time. But thanks to the DateTime module that comes pre-loaded with Python's standard utility modules we can easily manipulate date and time according to our own need
9 min read
Python SQLite - Working with Date and DateTime
SQLite does not support built-in DateTime storage a class, but SQLite allows us to work with timestamp types. We can store and retrieve Python date and datetime information stored in the SQLite database tables by converting them to Python date and datetime types and vice-versa. While inserting the datetime value, the python sqlite3 module converts
4 min read
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date from Pandas in Python. Method 1: Convert DateTime t
3 min read
Python - Convert excel serial date to datetime
This article will discuss the conversion of an excel serial date to DateTime in Python. The Excel "serial date" format is actually the number of days since 1900-01-00 i.e., January 1st, 1900. For example, the excel serial date number 43831 represents January 1st, 2020, and after converting 43831 to a DateTime becomes 2020-01-01. By using xlrd.xldat
3 min read
Convert Date To Datetime In Python
When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or working with data, mastering this conversion skill is
3 min read
How to Convert Datetime to Date in Pandas ?
DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime using the Pandas Python module. Syntax: yyyy-mm-dd H
4 min read
How to Convert String to Date or Datetime in Polars
When working with data, particularly in CSV files or databases, it's common to find dates stored as strings. If we're using Polars, a fast and efficient DataFrame library written in Rust (with Python bindings), we'll often need to convert these strings into actual date or datetime objects for easier manipulation and analysis. In this article, we'll
5 min read
Isoweekday() Method Of Datetime Class In Python
Isoweekday() is a method of the DateTime class that tells the day of the given date. It returns an integer that corresponds to a particular day. Syntax: datetime.isoweekday() Parameters: None Return Value: It returns an integer which corresponds to a day as per the table Integer ReturnedDay of the week1Monday2Tuesday3Wednesday4Thursday5Friday6Satur
3 min read
Isocalendar() Method Of Datetime Class In Python
The isocalendar() function is used to return a tuple of ISO Year, ISO Week Number, and ISO Weekday. Note: According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week.Therefore, ISO years always start with Monday.ISO year can start as yearly as 29th January or as late as 4th January of a Gregorian calendar.ISO years ca
2 min read
Isoformat() Method Of Datetime Class In Python
In this example, we will learn How to get date values in ISO 8601 format using Python. The Isoformat() function is used to return a string of date, time, and UTC offset to the corresponding time zone in ISO 8601 format. The standard ISO 8601 format is all about date formats for the Gregorian calendar. This format prescribes that a calendar date nee
4 min read
Python DateTime - Time Class
Time class represents the local time of the day which is independent of any particular day. This class can have the tzinfo object which represents the timezone of the given time. If the tzinfo is None then the time object is the naive object otherwise it is the aware object. Syntax: class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzi
4 min read
Python DateTime - Timedelta Class
Timedelta class is used for calculating differences between dates and represents a duration. The difference can both be positive as well as negative. Syntax: class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Example: C/C++ Code # Timedelta function demonstration from datetime import datetime, t
4 min read
Datetime.replace() Function in Python
Datetime.replace() function is used to replace the contents of the DateTime object with the given parameters. Syntax: Datetime_object.replace(year,month,day,hour,minute,second,microsecond,tzinfo) Parameters: year: New year value in range-[1,9999],month: New month value in range-[1,12],day: New day value in range-[1,31],hour: New hour value in range
2 min read
Python | datetime.timedelta() function
Python timedelta() function is present under datetime library which is generally used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations. Syntax : datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Ret
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg