ItSolutionStuff.com

How to Add Seconds to DateTime in Python?

By Hardik Savani • October 30, 2023
Python

This article goes in detailed on python add seconds to datetime example. I would like to share with you how to add seconds to date in python. We will look at example of how to add second to current date in python. you can see python add seconds to date in dataframe.

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.

Example 1: Python Add Seconds to DateTime

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDateString = "2022-06-01"
  
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-06-01 00:00:00
New Date :
2022-06-01 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-06-21 09:37:56.753762
New Date :
2022-06-21 09:38:46.753762

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Minutes to DateTime in Python?

Read Now →

Python POST Request with Parameters Example

Read Now →

Python Subtract Hours from Datetime Example

Read Now →

How to Add Hours to Date in Python?

Read Now →

How to Subtract Year to Date in Python?

Read Now →

How to Add Years to Date in Python?

Read Now →

How to Subtract Months to Date in Python?

Read Now →

How to Add Months to Date in Python?

Read Now →

How to Subtract Days from Date in Python?

Read Now →

How to Add Days to Date in Python?

Read Now →

How to Get Current Date and Time in Python?

Read Now →