To find the day of the week for a given date, we convert the date into a form that Python can understand and then determine which weekday it falls on. For example, the date "22 02 2003" corresponds to a Saturday.
Let’s explore simple and efficient ways to do this.
Using Zeller’s Congruence
Zeller’s Congruence is a manual formula to find the weekday for any date using day, month, year, and century. It returns a value from 0 to 6 (0 = Saturday, ..., 6 = Friday) without using date libraries.
d = "30 12 2003" # input
q, m, y = map(int, d.split()) # day, month, year
if m < 3:
m += 12
y -= 1
k = y % 100 # yearpart
j = y // 100 # century
h = (q + (13 * (m + 1)) // 5 + k + k // 4 + j // 4 + 5 * j) % 7
wd = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(wd[h])
Output
Tuesday
Explanation: This code extracts day, month and year from the input. January and February are treated as months 13 and 14 of the previous year. The year is split into k year part and j century . Zeller’s formula computes an index h (0 = Saturday to 6 = Friday), which is mapped to the weekday using a list.
Using calendar.weekday()
calendar.weekday(year, month, day) method returns the day of the week as an integer (0=Monday to 6=Sunday). You can map this number to the corresponding weekday using a custom list. This is an efficient method using Python's built-in calendar support.
import calendar
d = "03 02 2019" # input
q, m, y = map(int, d.split()) # day, month, year
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # days
print(wd[calendar.weekday(y, m, q)])
Output
Sunday
Explanation: calendar.weekday(y, m, q) returns an integer where 0 = Monday and 6 = Sunday and this integer is then used to access the corresponding weekday name from the wd list.
Using datetime.weekday()
datetime.weekday() method from the datetime module returns the weekday as an integer where Monday is 0 and Sunday is 6. You first convert a string date into a datetime object using strptime, then use weekday() to get the index of the day.
from datetime import datetime
d = "22 02 2003" # input
dt = datetime.strptime(d, "%d %m %Y")
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(wd[dt.weekday()])
Output
Saturday
Explanation: This code converts the date string to a datetime object, uses dt.weekday() to get an integer (0=Monday to 6=Sunday) and maps it to the weekday name from the list.
Using datetime.isoweekday()
datetime.isoweekday() method also comes from the datetime module and returns an integer representing the day of the week (1=Monday to 7=Sunday). It's similar to weekday() but follows ISO standards. Just subtract 1 to map it into a 0-based weekday list if needed.
from datetime import datetime
d = "22 12 2002" # input
dt = datetime.strptime(d, "%d %m %Y")
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(wd[dt.isoweekday() - 1])
Output
Sunday
Explanation: This code converts the string "22 12 2002" to a datetime object, uses dt.isoweekday() (1=Monday to 7=Sunday), subtracts 1 to make it zero-based and indexes the weekday name list.
Related Articles