As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.

The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.

A code example which uses the expression follows:

int d    = 15   ; //Day     1-31
int m    = 5    ; //Month   1-12`
int y    = 2013 ; //Year    2013` 

int weekday  = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;  

The expression uses the comma operator, as discussed in this answer.

Enjoy! ;-)

Answer from Thats it. on Stack Overflow
Top answer
1 of 16
34

As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.

The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.

A code example which uses the expression follows:

int d    = 15   ; //Day     1-31
int m    = 5    ; //Month   1-12`
int y    = 2013 ; //Year    2013` 

int weekday  = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;  

The expression uses the comma operator, as discussed in this answer.

Enjoy! ;-)

2 of 16
20

A one-liner is unlikely, but the strptime function can be used to parse your date format and the struct tm argument can be queried for its tm_wday member on systems that modify those fields automatically (e.g. some glibc implementations).

int get_weekday(char * str) {
  struct tm tm;
  memset((void *) &tm, 0, sizeof(tm));
  if (strptime(str, "%d-%m-%Y", &tm) != NULL) {
    time_t t = mktime(&tm);
    if (t >= 0) {
      return localtime(&t)->tm_wday; // Sunday=0, Monday=1, etc.
    }
  }
  return -1;
}

Or you could encode these rules to do some arithmetic in a really long single line:

  • 1 Jan 1900 was a Monday.
  • Thirty days has September, April, June and November; all the rest have thirty-one, saving February alone, which has twenty-eight, rain or shine, and on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

EDIT: note that this solution only works for dates after the UNIX epoch (1970-01-01T00:00:00Z).

🌐
Rosetta Code
rosettacode.org › wiki › Day_of_the_week
Day of the week - Rosetta Code
3 days ago - The program uses two ASSIST macro (XDECO,XPRNT) to keep the code as short as possible. * Day of the week 06/07/2016 DOW CSECT USING DOW,R15 base register LA R6,2008 year=2008 LOOP C R6,=F'2121' do year=2008 to 2121 BH ELOOP . LR R7,R6 y=year LA R8,12 m=12 LA R9,25 d=25 C R8,=F'3' if m<3 BNL MGE3 then LA R8,12(R8) m=m+12 BCTR R7,0 y=y-1 MGE3 LR R10,R7 y SRDA R10,32 .

As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.

The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.

A code example which uses the expression follows:

int d    = 15   ; //Day     1-31
int m    = 5    ; //Month   1-12`
int y    = 2013 ; //Year    2013` 

int weekday  = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;  

The expression uses the comma operator, as discussed in this answer.

Enjoy! ;-)

Answer from Thats it. on Stack Overflow
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 171273-c-program-date-day-week.html
c program for date to day of week
September 21, 2016 - #include <stdio.h> int main (void) { long int n; float n_day; int f, g; int month; int day; int year; char c; int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; char *days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int i; printf ("Welcome to the Date to Day-of-Week program.\n"); printf ("\nThe program will give the day of the week for any date from 1/1/1900\n"); printf ("\nEnter date (mm/dd/yyyy):"); scanf ("%i/%i/%i", &month, &day, &year); if ( month > 12) { printf ("Invalid month, please re-enter date"); printf ("\nEnter date (mm/dd/yyyy):"); scanf ("%i/%i/%i", &month, &day, &year); } if ( day > 31) { printf ("Invalid day, please re-enter date"); printf ("\nEnter date (mm/dd/yyyy):"); scanf ("%i/%i/%i", &month, &day, &year); } if ( year <= 1900) { printf ("Invalid year, please re-enter date.
🌐
Wikipedia
en.wikipedia.org › wiki › Determination_of_the_day_of_the_week
Determination of the day of the week - Wikipedia
January 21, 2026 - In numerical calculation, the days of the week are represented as weekday numbers. If Monday is the first day of the week, the days may be coded 1 to 7, for Monday through Sunday, as is practiced in ISO 8601. The day designated with 7 may also be counted as 0, by applying the arithmetic modulo ...
🌐
Scphillips
scphillips.com › units › dayform.html
Day of the Week - SCPhillips.com
This page will tell you what day of the week a date fell on, e.g. what day was 3rd March 1972?
🌐
Time and Date
timeanddate.com › calculators › weekday calculator: what day is it?
Weekday Calculator – What Day is this Date?
Finds the day of the week for any date. Find out which day of the week you were born, or if the Moon landing was on a Saturday or a Sunday.
🌐
Calculator.net
calculator.net › home › other › day of the week calculator
Day of the Week Calculator
Monday is the day of the week people tend to weigh the most. Monday can be alternatively known as "suicide day", due to the relatively higher number of suicides that take place on this weekday.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-day-of-the-week-for-a-given-date
Find day of the week for a given date - GeeksforGeeks
February 14, 2025 - The formula: (y + y/4 − y/100 + y/400 + t[m−1] + d) % 7 calculates the day of the week by summing the day (d), month code (t[m - 1]), and adjusted year values. The result is then taken modulo 7, which gives a value between 0 and 6, corresponding ...
Find elsewhere
🌐
Quora
quora.com › What-is-the-C-code-for-printing-the-days-of-the-week
What is the C code for printing the days of the week? - Quora
Answer (1 of 6): #include int main() { char weeks_days[7][10]={"Monday","Tuesday","Wednesday","Thrusday","Friday","Saturday","Sunday"}; int i=0; for(i=0;i
🌐
University of Waterloo
cs.uwaterloo.ca › ~alopez-o › math-faq › node73.html
How to determine the day of the week, given the month, day and year
For a Julian date, add 1 for 1700's, and 1 for every additional century you go back. Add the last two digits of the year. Divide by 7 and take the remainder. Now 1 is Sunday, the first day of the week, 2 is Monday, and so on.
🌐
Day of the Week
dayoftheweek.org
Day Of The Week, What Day Of The Week Is...
If you are trying to learn Spanish then this day of the week in Spanish is domingo. Here’s the March calendar.
🌐
The Old Farmer's Almanac
almanac.com › how-find-day-week
How to Find the Day of the Week for Any Date | The Old Farmer's Almanac
August 26, 2025 - Divide the sum by 7. The remainder is the day of the week! One is Sunday, two is Monday, and so on. If there is no remainder, the day is Saturday. [Note: At this step, it may be easiest to use long division to get the remainder.
🌐
The Old Farmer's Almanac
almanac.com › home › calendar › tools › day of the week calculator – what day is it?
Day of the Week Calculator – What Day Is It? | Almanac.com
August 14, 2025 - Get Almanac's Daily Updates Extended Forecasts Where to Buy Contact Us ... Planning a big day or digging into the past? Our Day of the Week Calculator makes it easy (and fun!) to find out what day of the week any date lands on: past, present, or future. Just pop in a date and get an instant answer.
Top answer
1 of 8
24

First: Do not write your own function, if there already are standardized functions that can handle the same problem. Point is that you might easily make a mistake (and I can already see one in the first line of your weekday() function as it is now), whereas implementations of standardized functions have been tested thoroughly and you can be confident that they deliver the result you are expected to get.

That being said, here is a possible approach using std::localtime and std::mktime:

#include <ctime>
#include <iostream>

int main()
{
  std::tm time_in = { 0, 0, 0, // second, minute, hour
      9, 10, 2016 - 1900 }; // 1-based day, 0-based month, year since 1900

  std::time_t time_temp = std::mktime(&time_in);

  //Note: Return value of localtime is not threadsafe, because it might be
  // (and will be) reused in subsequent calls to std::localtime!
  const std::tm * time_out = std::localtime(&time_temp);

  //Sunday == 0, Monday == 1, and so on ...
  std::cout << "Today is this day of the week: " << time_out->tm_wday << "\n";
  std::cout << "(Sunday is 0, Monday is 1, and so on...)\n";

  return 0;
}
2 of 8
8

New answer for old question because the tools they are a changing...

The C++20 spec says the following will have identical functionality to the intention of the code in the question:

#include <chrono>
#include <format>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    year_month_day dmy;
    cin >> parse("%d %m %Y", dmy);
    cout << format("{:%A}", weekday{dmy}) << '\n';
}

One can experiment today with this syntax by using this free, open-source date/time library, except that the date objects are in namespace date instead of namespace std::chrono, and the syntax of the format string is slightly altered.

#include "date/date.h"
#include <iostream>

int
main()
{
    using namespace std;
    using namespace date;
    year_month_day dmy;
    cin >> parse("%d %m %Y", dmy);
    cout << format("%A", weekday{dmy}) << '\n';
}
🌐
CoolConversion
coolconversion.com › home › day of the week calculator
Day of the Week Calculator
6 hours ago - Find out what day of the week any date falls on — past, present, or future. ... Select the month, day, and year from the fields above. The calculator will automatically display the day of the week for that date, as well as how long ago (or until) that date is.
🌐
Wikipedia
en.wikipedia.org › wiki › Week
Week - Wikipedia
2 days ago - Muslims observe their "day of congregation", known as yaum al-jum`ah, on Friday because it was described as a sacred day of congregational worship in the Quran. The English word week comes from the Old English wice, ultimately from a Common Germanic *wikōn-, from a root *wik- "turn, move, change".
🌐
TimeAndDate
timeanddate.com › calendar › days of the week
The Seven-Day Week
According to international standard ISO 8601, Monday is the first day of the week. It is followed by Tuesday, Wednesday, Thursday, Friday, and Saturday. Sunday is the 7th and last day of the week.
🌐
EnglishClub
englishclub.com › ref › esl › Power_of_7 › 7_Days_of_the_Week_2929.php
7 Days of the Week | Learn English
Days of the Week: Monday = associated ... with Venus Saturday = associated with Saturn Sunday = associated with the Sun · Weekdays: Monday, Tuesday, Wednesday, Thursday, Friday ......
🌐
DaniWeb
daniweb.com › programming › software-development › code › 216327 › day-of-week-given-a-date
c - Day of week given a date | DaniWeb
October 9, 2004 - Final modulo 7 gives the weekday. In short: vx = month key, ix = year-index + vx + (month>2), tx = ix + ix/4 + day, return tx % 7. ... The leap rule in the posted macro is wrong (it includes a 00 test) and will misclassify century years such as 2000. Use the plain rule in words: "leap if divisible by 4, except centuries not divisible by 400." Explicitly validate month range and reject out-of-range years/days.