Where:
- h is the day of the week (0 = Saturday, 1 = Sunday, ..., 6 = Friday)
- q is the day of the month
- m is the month (3 = March, 4 = April, ..., 14 = February; January and February are counted as months 13 and 14 of the previous year)
- K is the year of the century (i.e., year mod 100)
- J is the zero-based century (actually ⌊ year/100 ⌋)
Python Program to Find the Day of a Date.
We use Zeller's Congruence Algorithm to find the day of any date in Python. Below is the code implementation to do so.
Python Code.
# Python code to find day of week for any date def zellers_congruence(day, month, year): if month < 3: month += 12 year -= 1 K = year % 100 J = year // 100 h = (day + (13 * (month + 1)) // 5 + K + K // 4 + J // 4 - 2 * J) % 7 # List of day names days_of_week = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] # Adjust result (0 corresponds to Saturday) return days_of_week[(h + 5) % 7] # Example day = 25 month = 7 year = 2023 day_of_week = zellers_congruence(day, month, year) print(f"The day of the week for {day}-{month}-{year} is {day_of_week}.")
The day of the week for 25-7-2023 is Sunday.
This code takes a date as input and returns the day of the week using Zeller's Congruence. The result is an integer where 0 corresponds to Saturday, 1 to Sunday, and so on. We are using a list of days to replace the output integer with matching day of the week.
No comments:
Post a Comment