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.

 

 

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.

 

Matthias Bäuerlen Python Programmer

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top