- Related Questions & Answers
- Get the week number in MySQL for day of week?
- In MySQL, how we can compute date by providing the year, week number and day of the week? day of the week?
- C# Program to get current day of week
- How to get the day of the week in JavaScript?
- Get Day Number of Week in Java
- Finding day of week from date (day, month, year) in JavaScript
- Formatting day of week using SimpleDateFormat in Java
- Java Program to get day of week for the last day of each month in 2019
- How to get first day of the week using Python?
- Java Program to get the day of the week from GregorianCalendar
- Get the day of week for a particular date in Java
- Formatting day of week in EEEE format in Java
- Display Day Name of Week using Java Calendar
- How to get first day of week in PHP?
- Get the Day of the Week from Today's Date in Java

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a date (day, month and year). From this date, we have to find the day of the week of that given date. To solve this we will use Zeller’s Algorithm. The formula to find weekday using Zeller’s Algorithm is here

𝑤=$$\lgroup d+\lfloor \frac{13(m+1)}{5} \rfloor+y+\lfloor\frac{y}{4} \rfloor+\lfloor\frac{c}{4} \rfloor+5c \rgroup mod 7$$

The formula is containing some variables; They are −

**d** − The day of the date.

**m** − It is the month code. For March to December, it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then the given year will be decreased by 1.

**y** − Last two digits of the year

**c** − first two digits of the year

**w** − The weekday. When it is 0, it is Saturday, when it is 6, it means Friday

So for example, if we want to get the weekday of 4th January 1997, then the output will be “Saturday”

The algorithm is as follows −

**zellersAlgorithm(day, month, year)**

**Input** − The date of the day.

**Output** − Which day it was, (Sunday to Saturday).

Begin if month > 2, then mon := month else mon := 12 + month decrease year by 1 y := last two digit of the year c := first two digit of the year w := day + floor((13*(mon+1))/5) + y + floor(y/4) + floor(c/4) + 5*c w := w mod 7 return weekday[w] //weekday will hold days from Saturday to Friday End

#include #include using namespace std; string weekday[7] = {"Saturday","Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday"}; string zellersAlgorithm(int day, int month, int year){ int mon; if(month > 2) mon = month; //for march to december month code is same as month else{ mon = (12+month); //for Jan and Feb, month code will be 13 and 14 year--; //decrease year for month Jan and Feb } int y = year % 100; //last two digit int c = year / 100; //first two digit int w = (day + floor((13*(mon+1))/5) + y + floor(y/4) + floor(c/4) + (5*c)); w = w % 7; return weekday[w]; } int main(){ int day, month, year; cout << "Enter Day: "; cin >>day; cout << "Enter Month: "; cin >>month; cout << "Enter Year: "; cin >>year; cout << "It was: " <<zellersAlgorithm(day, month, year); }

(4, 1, 1997)

Enter Day: 4 Enter Month: 1 Enter Year: 1997 It was: Saturday

Advertisements