Date Conversion
Programming
Time Calculation
Scripting
Weekdays

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.

Browse interview questions

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:

h=(q+⌊13(m+1)5⌋+K+⌊K4⌋+⌊J4⌋−2J)mod  7h = (q + \left\lfloor\frac{13(m+1)}{5}\right\rfloor + K + \left\lfloor\frac{K}{4}\right\rfloor + \left\lfloor\frac{J}{4}\right\rfloor - 2J) \mod 7

Where:

  • hh is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, etc.)
  • qq is the day of the month
  • mm is the month (3 = March, 4 = April, ..., 12 = December). January and February are counted as months 13 and 14 of the previous year.
  • KK is the year of the century (yearmod  100\text{year} \mod 100).
  • JJ is the zero-based century (actually ⌊year/100⌋\left\lfloor \text{year} / 100 \right\rfloor).

For example, to find the day of the week for May 9, 2023:

  1. Convert the date to fit the formula: The year is 2023, the month is 5, and the day is 9.
  2. Plug these values into Zeller's formula to calculate hh.

Calculation Using Software Tools

Python

The datetime module in Python can be used to easily find the day of the week:

python
import datetime
date = datetime.datetime(2023, 5, 9)
print(date.strftime("%A"))

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:

javascript
var date = new Date(2023, 4, 9); // Note: JavaScript counts months from 0
console.log(date.toLocaleString('en-us', {weekday: 'long'}));

This will output Tuesday as well.

Using Microsoft Excel

Excel provides a WEEKDAY function that returns the day of the week as a number:

 
=WEEKDAY("5/9/2023", 1)

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:

MethodTool/ApproachOutput ExampleOutput Meaning
MathematicalZeller's Congruence2Monday
Pythondatetime module'Tuesday'String name
JavaScriptDate object'Tuesday'String name
SpreadsheetExcel WEEKDAY function3Numeric 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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions