Python Subtract Days from Date Example Tutorial

Published On: 05/09/2022 | Category: Python


Hi Guys,

In this post, we will learn python subtract days to date string. it's simple example of how to subtract days to date in python. This article will give you simple example of python minus 1 day. you can understand a concept of python minus days from date. Let's see bellow example python subtract 10 day 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.

So let's see bellow example:

Example 1: main.py
from datetime import datetime
from datetime import timedelta
  
myDateString = "2022-09-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-09-01 00:00:00
New Date :
2022-08-27 00:00:00
Example 2: 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-09-05 04:41:13.718118
New Date :
2022-08-31 04:41:13.718118

It will help you....