Python Sub Days in Date Example Tutorial

Published On: 08/06/2022 | Category: Python

Hi Dev,

In this example, you will learn python date sub days. you will learn how to sub days to the current date in python. Here you will learn how to sub days date in python datetime. This article will give you simple example of sub days to date python pandas. Let's get started with sub days to date python datetime.

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

Example : 1
from datetime import datetime,timedelta

currentTimeDate = datetime.now() - timedelta(5)
subDate = currentTimeDate.strftime('%Y-%m-%d')

print(subDate)
Output:
2022-06-03
Example : 2
from datetime import timedelta, date

currentTimeDate = date.today() - timedelta(3)
subDate = currentTimeDate.strftime('%Y-%m-%d')

print(subDate)
Output:
2022-06-05
Example : 3
import pandas as pd

my_date = "2022-06-08"
res_date = pd.to_datetime(my_date) - pd.DateOffset(days=10)
req_date = res_date.strftime('%Y-%m-%d')
print(res_date)
Output:
2022-05-29 00:00:00

It will help you....