To add seconds to the current time in Python we have three methods: the timedelta() libraries, the Pandas library, and the relativedelta library. Check out the details below to get more information.
Add seconds to the current time in Python
Using the timedelta() libraries
To add seconds to the current time in Python, you could use the timedelta() libraries in Python.
Syntax:
datetime.timedelta()
Return value: return value is a Date object.
Example:
Firstly, we must import them. Then we use datetime.now()
to get the current date time. Finally, we add 3 seconds to the today object by using the timedelta()
method. Let’s see the code example for more information.
from datetime import datetime, timedelta today = datetime.now() print ("Today's date and time:",today) print ("Today's time is:", f'{today:%H:%M:%S}') # Add 3 seconds to the current time newTime = today + timedelta(seconds=3) print ("After adding 3 seconds to time:", f'{newTime:%H:%M:%S}')
Output:
Today's date and time: 2022-10-06 12:28:06.989097
Today's time is: 12:28:06
After adding 3 seconds to time: 12:28:09
Using the Pandas library
Firstly, we must import them. Then we use datetime.now() to get the current date time. Finally, we add 5 seconds to today object by using the pandas.to_timedelta
method and unit as s (seconds). Let’s see the code example for more information.
Syntax:
pandas.to_timedelta(Data, Unit='ns', Errors='raise')
Parameters:
- Data: data to be converted to time delta.
- Unit: Denotes the unit of the data.
- Errors: have three values ignore, raise, coerce. The default value is ‘raise.’
Return value: return value is timedelta64 or numpy.array.
Example:
from datetime import datetime import pandas as pandas today = datetime.now() print ("Today's date and time:",today) print ("Today's current time is:", f'{today:%H:%M:%S}') # Add 5 seconds to the current time newTime = today + pandas.to_timedelta(5, unit = 's') print ("After adding 5 seconds to the current time:", f'{newTime:%H:%M:%S}')
Output:
Today's date and time: 2022-10-06 12:38:31.753153
Today's current time is: 12:38:31
After adding 5 seconds to the current time: 12:38:36
Using the relativedelta library
Another simple way to add seconds to the current time in Python is to use the relativedelta library.
Example:
In this example, we must import the libraries first. Then we use datetime.now() to get the current date time. Finally, we add 5 seconds to the today object by using the relativedelta library in Python by adding the second’s parameter as five and storing the result to the new_time variable. Let’s see the code example for more information.
from datetime import datetime from dateutil.relativedelta import relativedelta today = datetime.now() print ("Today's date and time:",today) print ("Today's current time is:", f'{today:%H:%M:%S}') # Add 5 seconds to the current time newTime = today + relativedelta(seconds = 5) print ("After adding 5 seconds to the current time:", f'{newTime:%H:%M:%S}')
Output
Today's date and time: 2022-10-06 12:28:06.989097
Today's time is: 12:28:06
After adding 5 seconds to time: 12:28:11
Summary
In this tutorial, we have explained how to add seconds to the current time in Python. We always hope this tutorial is helpful to you. Leave your comment here if you have questions or comments about improving the article. Thanks for reading!
Maybe you are interested:
- Mapping over dictionary values in Python
- Add seconds to datetime in Python
- Add days to a date in Python
- Add hours to datetime in Python

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python