11-Mar-2022
.
Admin
Hi Guys,
In this blog, I will learn how to get a start and end date of the week in python. we will talk about python's get start date and ending date. You can get the start date and end date of the week using python.
This article will give you examples of how to get the start date and end date of the week using python. If you want to get a start and end date of the week in python then you can use this example.
In Python, we can get start date and end date of week using python. let's see the below example:
Example 1 :
# import datetime module
from datetime import datetime, timedelta
day = '27/01/2022'
dt = datetime.strptime(day, '%d/%m/%Y')
start = dt - timedelta(days=dt.weekday())
end = start + timedelta(days=6)
print("Week Start Date:"+str(start))
print("Week End Date:"+str(end))
Output
Week Start Date:2022-01-24 00:00:00
Week End Date:2022-01-30 00:00:00
Example 2 :
# import datetime module
from datetime import date, timedelta
today = date.today()
start = today - timedelta(days=today.weekday())
end = start + timedelta(days=6)
print("Today: " + str(today))
print("Week Start: " + str(start))
print("Week End: " + str(end))
Output
Today: 2022-01-28
Week Start: 2022-01-24
Week End: 2022-01-30
It will help you....
#Python