How to Get Start Date and End Date of Week using Python?
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:
# 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))
Week Start Date:2022-01-24 00:00:00
Week End Date:2022-01-30 00:00:00
# 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))
Today: 2022-01-28
Week Start: 2022-01-24
Week End: 2022-01-30
✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.
You might also like.
Get date from week number
A week number is not enough to generate a date; you need a day of the week as well. Add a default:
import datetime d = "2013-W26" r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w") print(r)
The -1 and -%w pattern tells the parser to pick the Monday in that week. This outputs:
%W uses Monday as the first day of the week. While you can pick your own weekday, you may get unexpected results if you deviate from that.
See the strftime() and strptime() behaviour section in the documentation, footnote 4:
When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.
Note, if your week number is a ISO week date, you’ll want to use %G-W%V-%u instead! Those directives require Python 3.6 or newer.
Does not work. datetime.datetime.strptime(«2018-W0-0», «%Y-W%W-%w») == datetime.datetime.strptime(«2018-W1-0», «%Y-W%W-%w») —> «2018-01-07 00:00:00». 1st and 2nd week of 2018 have the same start date.
@ozw1z5rd: that’s because there is no week 0 in 2018. Week 0 is the partial week before the first Monday of the year; in 2018 the first Monday is the 1st of January. In other words, your input string is the problem here, not the technique, and Python has error-corrected to interpret W0 to mean W1 instead.
@ozw1z5rd: If this is an issue for your app, you’ll need to special-case such years; if d.endswith(‘-W0’) and r.day == 7: raise ValueError(‘Invalid date string, No week 0 in <>‘.format(r.year))
In Python 3.8 there is the handy datetime.date.fromisocalendar :
>>> from datetime import date >>> date.fromisocalendar(2020, 1, 1) # (year, week, day of week) datetime.date(2019, 12, 30, 0, 0)
In older Python versions (3.7-) the calculation can use the information from datetime.date.isocalendar to figure out the week ISO8601 compliant weeks:
from datetime import date, timedelta def monday_of_calenderweek(year, week): first = date(year, 1, 1) base = 1 if first.isocalendar()[1] == 1 else 8 return first + timedelta(days=base - first.isocalendar()[2] + 7 * (week - 1))
Both works also with datetime.datetime .
Get Start & End of Week Given a datetime Object in Python (Example)
In this Python post, you’ll learn how to print the start and end of a week based on a datetime object.
The tutorial contains the following:
Let’s jump right to the example!
Creation of Exemplifying Data
We have to import the datetime module, as you can see here:
import datetime # Import datetime module
I’ll use the following data as a basement for this Python programming tutorial:
my_dt = datetime.datetime(2024, 6, 21, 10, 12, 53) # Creating example datetime print(my_dt) # 2024-06-21 10:12:53
Have a look at the previous Python console output. It shows that our example datetime object is Friday, June 21, 2024, 10:12:53.
Example: Get Start & End Of Week from Given Date
In this example, I’ll illustrate how to find out the start and end of the week from our previous created datetime object.
For this, we also have to import the timedelta function, consider the Python code below:
from datetime import timedelta
Since we just need the components year, month, and day to identify the beginning and end of a week, we extract them from our datetime object like you can see here:
my_dt_trunc = datetime.date(my_dt.year, # Truncate time component my_dt.month, my_dt.day) print(my_dt_trunc) # 2024-06-21
Next, we can combine the timedelta and weekday functions to return the start date of the week:
start_of_week = my_dt_trunc - timedelta(days = my_dt_trunc.weekday()) # timedelta & weekday print(start_of_week) # Return Start of Week # 2024-06-17
As you can see on the output above, the week starts at 2024-06-17, of course it’s a Monday.
From here, we can simply get to the end of the week by adding a timedelta containing 6 days, consider the following Python syntax:
end_of_week = start_of_week + timedelta(days = 6) # timedelta function print(end_of_week) # Return End of Week # 2024-06-23
Now we also have the end date of our week, in this case 2024-06-23 (Sunday).
Video, Further Resources & Summary
Would you like to know more about the extraction of the start and end of a week given by a datetime object? Then you might want to have a look at the following video on my YouTube channel. In the video, I’m explaining the Python code of the present tutorial in a live session in Python.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Accept YouTube Content
Besides that, you might have a look at some of the other tutorials on my website:
This post has illustrated how to return the beginning and end of a week given a datetime object in the Python programming language. In case you have additional questions and/or comments, tell me about it in the comments.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.