Kodeclik Logo

Our Programs

Courses

Gifting

Learn More

Schedule

Kodeclik Blog

Find the day of the week in Python

In Python, there are various ways to find the day of the week for a given date. In this blog post, we will explore some of the most popular methods to achieve this.

If you are a Star Wars fan, you might like to know what day May 4, 2024 will fall on. Let us learn how to find out!

How to find the day of the week in Python from a given date

Method 1: Use the weekday() method of the datetime module

The datetime module in Python provides classes for working with dates and times. To find the day of the week for a given date, we can use the weekday() method, which returns an integer representing the day of the week. The integer values returned by the weekday() method are to be interpreted as follows: 0 for Monday, 1 for Tuesday, and so on till 6 for Sunday.

Here is an example of how to find the day of the week for a given date using the datetime module:

import datetime
my_date = datetime.date(2024, 5, 4)

day_of_week = my_date.weekday()

print("My date:", my_date)
print("Day of the week:", day_of_week)

The output is:

My date: 2024-05-04
Day of the week: 5

From this you can figure out that May 4, 2024 falls on a Saturday.

Method 2: Use the isoweekday() method of the datetime module

The second method is almost exactly the same except with isoweekday() the convention is that 1 refers to Monday and 7 refers to Sunday. Let us try that out!

import datetime

my_date = datetime.date(2024, 5, 4)

day_of_week = my_date.isoweekday()

print("My date:", my_date)
print("Day of the week:", day_of_week)

As expected the output is:

My date: 2024-05-04
Day of the week: 6

Note that 6 now denotes Saturday (not Sunday per the previous convention).

Method 3: Use the weekday() method of the calendar module

If the numbers for days conventions get confusing, you can use the calendar module, which provides various functions related to calendars, including weekday names and abbreviations.

Here is an example of how to find the day of the week for a given date using the calendar module:

import datetime
import calendar

my_date = datetime.date(2024, 5, 4)

weekday_name = calendar.day_name[my_date.weekday()]
print(weekday_name)

Here we are using two methods from the calendar module. First we use the weekday() method of the calendar module to find the day of the week for the given date. Second we use the day_name list of the calendar module to find the name of the weekday and assign the result to a variable weekday_name.

The output is:

Saturday

Finding the day of the week for a given date in Python is thus a very easy task that can be achieved using various methods. In this blog post, we have explored three popular methods using the datetime and calendar modules. Which one is your favorite?

If you liked this blogpost, tinker more with dates, and learn how to add a specified number of days to a date and compute a new date.

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.