Prerequisites: Python datetime module with examples
In Python, the
datetime module is used to operate with date and time objects. This module has different functions and classes for working with date and time parsing, formatting and arithmetic operations. The datetime module has various constants such as
MINYEAR
,
MAXYEAR
etc, various classes like
datetime.date
,
datetime.time
,
datetime.datetime
etc and various instance methods such as
replace()
,
weekday()
,
isocalendar()
etc of date object and
time()
,
dst()
,
timestamp()
, etc. of time object.
Timetupe()
In order to use the
timetuple()
method we need to import the datetime module. The
timetuple() methodĀ is an instance method for the datetime module this method returns
time.struct_time
object. TheĀ attributes of the
time.struct_time
object can be accessed by index or name of the attribute.
struct_time
object has attributes for representing both date and time fields, these attributes are stored in tuples:
Index |
Attribute |
Field |
Domain |
0 |
4 Digit Year |
tm_year |
Example: 2020 |
1 |
Month |
tm_mon |
1 to 12 |
2 |
Day |
tm_mday |
1 to 31 |
3 |
Hour |
tm_hour |
0 to 23 |
4 |
Minute |
tm_min |
0 to 59 |
5 |
Second |
tm_sec |
0 to 61 |
6 |
Day of Week |
tm_wday |
0 to 6 |
7 |
Day of Year |
tm_yday |
1 to 366 |
8 |
Daylight Saving Time |
tm_isdst |
-1, 0, 1 |
Note: The
tm_isdst
attribute is a flag which is set to 0 when daylight saving time active is off, 1 if daylight saving time is active and -1 if daylight saving time is determined by the compiler, the
tm_yday
attribute is based on Julian Day Number and in
tm_sec
, values 60 or 61 are leap seconds.
Example 1: Below example shows the time tuple of the present date.
Python3
# program to illustrate timetuple()
# method in Python
import datetime
# creating object
obj = datetime.datetime.today()
# obtaining the attributes of the
# datetime instance as a tuple
objTimeTuple = obj.timetuple()
# displaying the tuples of the object
print(objTimeTuple)
Output:
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=29, tm_hour=14, tm_min=13, tm_sec=32, tm_wday=2, tm_yday=29, tm_isdst=-1)
In the above program, the
datetime
object
obj
is assigned with the present date and then
timetuple()
method is used to obtain the attributes of
obj
in tuples and is displayed. We can also use a loop to display the attributes of
obj
.
Example 2: Let us look at another example where we use a custom date.
Python3
# program to illustrate timetuple()
# method in Python
import datetime
# creating object and initializing
# it with custom date
birthDate = datetime.datetime(1999, 4, 6)
# obtaining the attributes and displaying them
print("Year: ", birthDate.timetuple()[0])
print("Month: ", birthDate.timetuple()[1])
print("Day: ", birthDate.timetuple()[2])
print("Hour: ", birthDate.timetuple()[3])
print("Minute: ", birthDate.timetuple()[4])
print("Second: ", birthDate.timetuple()[5])
print("Day of Week: ", birthDate.timetuple()[6])
print("Day of Year: ", birthDate.timetuple()[7])
print("Daylight Saving Time: ", birthDate.timetuple()[8])
Output:
Year: 1999
Month: 4
Day: 6
Hour: 0
Minute: 0
Second: 0
Day of Week: 1
Day of Year: 96
Daylight Saving Time: -1
Here the
datetime
object
birthDate
is assigned with a custom date and then each attribute of that object is displayed by the index of the tuple.The parameters retrieved from the
time.struct_time
structure can be accessed as elements of tuple and the year, month, and day fields assigned as the datetime object(here 1999, 4 and 6) and fields referring to the hour, minutes, seconds are set to zero.
Similar Reads
Python time.tzset() Function
In Python, the tzset() function of the time module is based on the re-initialization settings using the environment variable TZ. tzset() method of time module in python resets the time transformation protocol. this timezone means non-DST seconds west of UTC time and altzone means DST seconds west of
3 min read
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 obje
2 min read
Python | time.time() method
Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.time() method of Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform dependent. Note: The epoch is the point where the time
2 min read
Python | time.time_ns() method
Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.time_ns() method of Time module is used to get the time in nanoseconds since the epoch. To get the time in seconds since the epoch, we can use time.time() method. The epoch
2 min read
time.sleep() in Python
Python time sleep() function suspends execution for the given number of seconds. Syntax of time sleep() Syntax : sleep(sec) Parameters : sec : Number of seconds for which the code is required to be stopped. Returns : VOID. Sometimes, there is a need to halt the flow of the program so that several
4 min read
Python time strptime() function
The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it. It raises ValueError if the string cannot be formatted according to the provided directives
2 min read
Get UTC timestamp in Python
UTC timestamp represents a specific point in time as measured from the "Unix epoch" â January 1, 1970, 00:00:00 UTC. This timestamp is often used in computing because it is not affected by time zones or daylight saving time, providing a consistent reference time across the globe. When working with t
3 min read
Python | Pandas Timestamp.time
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.time() function return time object with same time but with tzinfo equ
2 min read
Python | Pandas Timestamp.week
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.week attribute return an integer value which is the ordinal value of
2 min read
Python | Pandas Timestamp.year
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.year attribute return the year in which the date in the given Timesta
2 min read