How do I get the day of week given a date?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
To determine the day of the week from a given date, various methods can be employed ranging from manual calculations to the use of computer programming languages. Fundamentally, this process involves understanding the relationship between the calendar date and the days of the week.
Manual Calculation: Zeller's Congruence
One of the most famous algorithms for finding the day of the week is Zeller's Congruence. This formula is applicable for both the Gregorian and Julian calendars. The formula can be represented as:
Where:
- is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, etc.)
- is the day of the month
- is the month (3 = March, 4 = April, ..., 12 = December). January and February are counted as months 13 and 14 of the previous year.
- is the year of the century ().
- is the zero-based century (actually ).
For example, to find the day of the week for May 9, 2023:
- Convert the date to fit the formula: The year is 2023, the month is 5, and the day is 9.
- Plug these values into Zeller's formula to calculate .
Calculation Using Software Tools
Python
The datetime module in Python can be used to easily find the day of the week:
This will output Tuesday, which is the day of the week for May 9, 2023.
JavaScript
JavaScript provides a Date object which can be used similarly:
This will output Tuesday as well.
Using Microsoft Excel
Excel provides a WEEKDAY function that returns the day of the week as a number:
Here, the second parameter defines the starting day (1 = Sunday, 2 = Monday, etc.). This formula will output 3, representing Tuesday.
Summary Table
Here's a comparison of methods and tools used to determine the day of the week from a date:
| Method | Tool/Approach | Output Example | Output Meaning |
| Mathematical | Zeller's Congruence | 2 | Monday |
| Python | datetime module | 'Tuesday' | String name |
| JavaScript | Date object | 'Tuesday' | String name |
| Spreadsheet | Excel WEEKDAY function | 3 | Numeric day (Tue) |
Conclusion
Determining the day of the week from a specific date can be approached in various ways depending on the tools and complexity desired. For quick, accurate, and repeatable results, using programming languages or spreadsheet tools is preferable. For a deeper understanding or manual calculations, methods like Zeller's Congruence are invaluable. Each method has its unique application and suitability depending on the context. Whether you are programming, planning schedules, or just curious, knowing how to find the day of the week from a date is a handy skill.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.