ItSolutionStuff.com

How to Subtract Days from Date in Python?

By Hardik Savani • October 30, 2023
Python

In this tutorial we will go over the demonstration of python subtract days to date example. Here you will learn how to subtract days to date in python. this example will help you python minus 1 day. This article goes in detailed on python minus days from date. follow bellow step for python subtract days to date string.

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

Example 1: Python Subtract Days to Date String

main.py

from datetime import datetime
from datetime import timedelta
  
myDateString = "2022-06-01"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
newDate = myDate - timedelta(days=5)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-01 00:00:00
New Date :
2022-05-27 00:00:00

Example 2: Python Subtract Days to Today's Date

main.py

from datetime import datetime
from datetime import timedelta
   
myDate = datetime.today()
  
newDate = myDate - timedelta(days=5)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-11 09:37:55.350531
New Date :
2022-06-06 09:37:55.350531

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

Python Get Day Name from Number Example

Read Now →

Python Get Month Name from Number Example

Read Now →

How to Get Yesterday Date in Python?

Read Now →

How to Get Current Hour in Python?

Read Now →

How to Get Current Time in Python?

Read Now →

How to Get Current Month in Python?

Read Now →

How to Get Current Day in Python?

Read Now →

How to Get Current Year in Python?

Read Now →

How to Get Current Date and Time in Python?

Read Now →

How to Get Current Date in Python?

Read Now →