Add Seconds, Minutes & Hours to datetime Object in Python (3 Examples)
The table of content is structured as follows:
Let’s dive into it!
Importing datetime Module & Creating Example Date & Time Object
Before we can start, we need to load the datetime module to Python:
Next, we have to create an exemplifying datetime object:
my_date = datetime.datetime(2023, 5, 16, 23, 32, 17) print(my_date) # 2023-05-16 23:32:17
The previous console output shows that we have created a datetime object representing the 16th of May 2023 at 23 hours, 32 minutes, and 17 seconds.
Adding Seconds to datetime Object
In the first example, we’ll add seconds to our datetime object.
For this task, we can use the timedelta() function and the seconds argument as shown below:
my_date_seconds = my_date + datetime.timedelta(seconds = 20) print(my_date_seconds) # 2023-05-16 23:32:37
As you can see, we have created a new datetime object showing a date and time 20 seconds later compared to our input datetime object.
Adding Minutes to datetime Object
Similar to Example 1, we can use the minutes argument to add minutes to a datetime object:
my_date_minutes = my_date + datetime.timedelta(minutes = 5) print(my_date_minutes) # 2023-05-16 23:37:17
The previously shown date and time is 5 minutes later than the input date.
Adding Hours to datetime Object
Following the same style as Examples 1 and 2, we can use the timedelta() function and the hours argument to add hours to a datetime object:
my_date_hours = my_date + datetime.timedelta(hours = 30) print(my_date_hours) # 2023-05-18 05:32:17
The previously created datetime object is 30 hours later than the input date and time.
Video, Further Resources & Summary
Do you need more explanations on datetime objects? Then you may have a look at the following video of the Socratica YouTube channel.
The video explains how to use the datetime module to handle dates and times in Python.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the other Python tutorials provided on Statistics Globe:
- Add Days, Months & Years to datetime Object in Python
- Calculate Time Difference Between Two datetime Objects in Python
- Convert datetime into String with Milliseconds
- Convert datetime Object to Seconds, Minutes & Hours
- Convert datetime to String without Microsecond Component
- Python Programming Tutorials
Summary: This post has shown you how to add seconds, minutes, and hours to datetime objects in the Python programming language. In case you have any additional questions, you may leave them in the comment section below.
Note: This article was created in collaboration with Gottumukkala Sravan Kumar. Gottumukkala is a data analyst and programmer who helps to create tutorials on topics such as the datetime module in Python. You may find more information about Gottumukkala and his other articles on his profile page.