Python Add Seconds to DateTime Example Tutorial

Published On: 20/09/2022 | Category: Python


Hi Guys,

Now, let's see article of how to add seconds to date in python. This article goes in detailed on python add seconds to datetime example. I would like to show you python add seconds to date in dataframe. this example will help you python add 1 second to date string.

In this example, I will give two examples for you of how to add seconds to date in python and how to add seconds to today's datetime in python. therefore, let's see below example code and try it.

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

Example 1: Python Add Seconds to DateTime

main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDateString = "2022-09-20"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
addSecondsNumber = 50;
newDate = myDate + relativedelta(seconds=addSecondsNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)
Output:
Old Date :
2022-09-20 00:00:00
New Date :
2022-09-20 00:00:50

Example 2: Python Add Seconds to Current DateTime

main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDate = datetime.today()
  
addSecondsNumber = 50;
newDate = myDate + relativedelta(seconds=addSecondsNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)
Output:
Old Date :
2022-09-20 04:05:44.793490
New Date :
2022-09-20 04:06:34.793490

It will help you....

Happy Pythonic Coding!