Subtract Seconds, Minutes & Hours from datetime in Python (3 Examples)
In this Python tutorial you’ll learn how to get X seconds, minutes, and hours earlier as a certain datetime object.
The tutorial contains these contents:
So now the part you have been waiting for – the exemplifying Python code:
Example Data & Software Libraries
First, we need to load datetime and timedelta:
from datetime import datetime, timedelta # Import libraries |
from datetime import datetime, timedelta # Import libraries
Let’s also construct some example data in Python:
my_datetime = datetime.today() # Creating example datetime print(my_datetime) # Print example datetime # 2022-03-28 14:09:47.434807 |
my_datetime = datetime.today() # Creating example datetime print(my_datetime) # Print example datetime # 2022-03-28 14:09:47.434807
Have a look at the previous output of the Python console. It shows our created date and time object, i.e. the current date and time of today.
Example 1: Get datetime X Seconds Ago
In Example 1, I’ll show how to subtract seconds from a datetime object.
For this task, we can use the timedelta function and the second argument as demonstrated by the code below:
my_datetime_sec = my_datetime - timedelta(seconds = 50) # Calculate 50 seconds earlier print(my_datetime_sec) # Print new datetime object # 2022-03-28 14:08:57.434807 |
my_datetime_sec = my_datetime - timedelta(seconds = 50) # Calculate 50 seconds earlier print(my_datetime_sec) # Print new datetime object # 2022-03-28 14:08:57.434807
As you can see based on the output above, we have created a new data object called my_datetime_sec which shows our date and time 50 seconds earlier.
Example 2: Get datetime X Minutes Ago
In Example 2, I’ll illustrate how to construct a datetime object that contains a date and time that is a certain number of minutes earlier compared to an input date and time object.
To accomplish this, we can apply the timedelta function once again. This time, in combination with the minutes argument.
my_datetime_min = my_datetime - timedelta(minutes = 25) # Calculate 25 minutes earlier print(my_datetime_min) # Print new datetime object # 2022-03-28 13:44:47.434807 |
my_datetime_min = my_datetime - timedelta(minutes = 25) # Calculate 25 minutes earlier print(my_datetime_min) # Print new datetime object # 2022-03-28 13:44:47.434807
Considering the output above, a new datetime object has been created. The time in this object is 25 minutes earlier than in our input object.
Example 3: Get datetime X Hours Ago
The last example shows how to subtract a certain number of hours to get a previous date and time.
We can get there by using the timedelta function combined with the hours argument.
my_datetime_hrs = my_datetime - timedelta(hours = 4) # Calculate 4 hours earlier print(my_datetime_hrs) # Print new datetime object # 2022-03-28 10:09:47.434807 |
my_datetime_hrs = my_datetime - timedelta(hours = 4) # Calculate 4 hours earlier print(my_datetime_hrs) # Print new datetime object # 2022-03-28 10:09:47.434807
The above output shows our example date and time 4 hours earlier.
Video, Further Resources & Summary
If you need more information on the content of this post, I recommend watching the following video on my YouTube channel. In the video, I’m explaining the examples of this article.
The YouTube video will be added soon.
Furthermore, you might want to read the other posts on https://statisticsglobe.com/. You can find a selection of posts below.
- Convert datetime Object to Seconds, Minutes & Hours in Python
- Add Seconds, Minutes & Hours to datetime Object in Python
- Introduction to Python
In this Python tutorial you have learned how to subtract a specific number of seconds, minutes, and hours from a datetime object. The syntax of this tutorial can be used to get the very previous second, minute, or hour, as well as to find dates and times at an earlier point. Please let me know in the comments section, if you have additional questions.