How To Add Seconds From Date Time In Python?

18-Jan-2022

.

Admin

How To Add Seconds From Date Time In Python?

Hello Friends,

Now let's see example of how to add seconds from current datetime in python. I am going to show you python datetime to addition seconds example. We will learn addition seconds in datetime using python.

The datetime module is supplies classes for manipulating datetimes and times. This article will give you example of how to addition seconds to datetime in python.

Here I will give an example for how to addition seconds to datetime in python. I am use datetime and time module to addition seconds datetime. So let's see the below example:

Example : 1


example1.py

# import datetime module

from datetime import timedelta, date

currentTimeDate = datetime.now() + relativedelta(seconds=100)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example1.py

Output:

16:59:21

Example : 2

example2.py

# import datetime module

from datetime import datetime

from dateutil.relativedelta import relativedelta

currentTimeDate = datetime.now() + relativedelta(seconds=150)

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Run Example

python example2.py

Output:

17:00:10

Example : 3

example3.py

# import pandas module

import pandas as pd

initial_date = "2021-12-18 12:08:00"

req_date = pd.to_datetime(initial_date) + pd.DateOffset(seconds=200)

req_date = req_date.strftime('%H:%M:%S')

print(req_date)

Run Example

python example3.py

Output:

12:11:20

I hope it will help you....

#Python