14

What is the algorithm that, given a day, month and year, returns a day of the week?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 3
    On Wikipedia: [Calculating the day of the week](http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week). – hammar Apr 26 '11 at 23:54
  • 1
    You can Google for that in about five seconds. Here is another question on SO about it: http://stackoverflow.com/questions/3017745/given-date-get-day-of-week-systemtime –  Apr 26 '11 at 23:55
  • @darvid: That's a Windows-specific Q&A, I don't think it counts. – Potatoswatter Apr 26 '11 at 23:57
  • @darvids That question (at least the answers) is about WinAPI and not an algorithm. – kiw Apr 26 '11 at 23:57
  • 2
    @hammar: Most questions can be answered outside of the site. That's besides the point. – Potatoswatter Apr 26 '11 at 23:57
  • 1
    @Potatoswatter: I still think it's reasonable to do a quick search before asking. In this case the first hit on Google turns up an answer. – hammar Apr 27 '11 at 00:01
  • 2
    @hammar: The [FAQ](http://stackoverflow.com/faq) says nothing about asking easy questions. If you don't want to answer it, then don't. This isn't a duplicate. – Potatoswatter Apr 27 '11 at 00:04

3 Answers3

23

This can be done using the std::mktime and std::localtime functions. These functions are not just POSIX, they are mandated by the C++ Standard (C++03 §20.5).

#include <ctime>

std::tm time_in = { 0, 0, 0, // second, minute, hour
        4, 9, 1984 - 1900 }; // 1-based day, 0-based month, year since 1900

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

// the return value from localtime is a static global - do not call
// this function from more than one thread!
std::tm const *time_out = std::localtime( & time_temp );

std::cout << "I was born on (Sunday = 0) D.O.W. " << time_out->tm_wday << '\n';
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 3
    +1 upvoted. Also voted to reopen this question. Perhaps it is a stack overflow duplicate, I don't know. But this is a real C++ question (was closed as not a real question), and this is a good answer to it. – Howard Hinnant Apr 27 '11 at 00:51
  • Hmm, got a downvote. Not sure why, but note that `std::localtime` is not thread-safe. See [this Q&A](http://stackoverflow.com/q/7313919/153285). – Potatoswatter Mar 26 '14 at 09:55
  • I know its accepted answer but not the answer he really asked for. – Cem Kalyoncu Mar 26 '14 at 18:43
1

One of the easiest algorithm for this is Tomohiko Sakamoto Algorithm:

static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

Check this out: https://iq.opengenus.org/tomohiko-sakamoto-algorithm/

I found Wang's method also interesting

w = (d - d^(m) + y^ - y* + [y^/4 - y*/2] - 2( c mod 4)) mod 7

http://rmm.ludus-opuscula.org/PDF_Files/Wang_Day_5_8(3_2015)_high.pdf This pdf is really helpful too.

Thanks!

0

You need a starting point. Today is fine. Hard-code it.

Then, you need to represent the number of days in a month. This is 31, 28, 31, 30, 31, 30, ... . So you can start adding and subtracting 365 % 7 to the day of the week for each year, and (sum of days in difference of month) % 7 again. And so on.

The caveat: Leap years occur on every 4th year, but not every 100th, unless that year is also a multiple of 400.