Python Subtract Seconds from DateTime Example Tutorial
Published On: 21/09/2022 | Category:
Python

Hi Guys,
Today, how to subtract second to current date in python is our main topic. you'll learn python 3 subtract seconds to datetime string. Here you will learn python subtract seconds to datetime example. I would like to share with you how to subtract seconds to date in python.
In this example, I will give two examples for you of how to subtract seconds to date in python and how to subtract 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.pyfrom datetime import datetime from dateutil.relativedelta import relativedelta myDateString = "2022-09-21" myDate = datetime.strptime(myDateString, "%Y-%m-%d") subSecondsNumber = 50; newDate = myDate - relativedelta(seconds=subSecondsNumber) print("Old Date :") print(myDate) print("New Date :") print(newDate)Output:
Old Date : 2022-09-21 00:00:00 New Date : 2022-09-20 23:59:10
Example 2: Python Add Seconds to Current DateTime
main.pyfrom datetime import datetime from dateutil.relativedelta import relativedelta myDate = datetime.today() subSecondsNumber = 50; newDate = myDate - relativedelta(seconds=subSecondsNumber) print("Old Date :") print(myDate) print("New Date :") print(newDate)Output:
Old Date : 2022-09-21 14:15:13.586920 New Date : 2022-09-21 14:14:23.586920
It will help you....
Happy Pythonic Coding!