Skip to content

All topics  /  Python

How To Substract Seconds From Date Time In Python?

Python ·

Hello Friends,

Now let's see example of how to substract seconds from current datetime in python. I am going to show you python datetime to substraction seconds example. We will learn substraction 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 substraction seconds to datetime in python.

Here I will give an example for how to substraction seconds to datetime in python. I am use datetime and time module to substraction 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:

12:32:45

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:

12:31:55

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:04:40

It will help you....