Anyone have a sketch to calculate the day of week based on the date?

Satbeginner:
Meanwhile I did some 'googleling' and found this page:

Determination of the day of the week - Wikipedia

where they have a real lot information on the DayOfWeek topic.

From there I got this bit of (very compact!) code that does work too

Hmm, sounds like that should've been step one

jurs:
The code posted in #13 calculates wrong results in several cases.

Give it a try with this test code:

void setup()

{
Serial.begin(9600);
Serial.println(calcDayOfWeek(1999,12,31));

Serial.println(calcDayOfWeek(2000,1,1));

}




DayOfWeek is surely never 7, it has to be in the range 0 to 6 only!

I think I intentionally used 1 to 7 for that.

Hello

Here is my tiny code with very few 8-bits calculations.
It works fine with RTCs (no century handling)

uint8_t calcDayOfWeek(uint8_t y, uint8_t m, uint8_t d) {
  uint8_t dow = y + (y >> 2) + d;
  switch (m) {
    case  2 :
    case  3 :
    case 11 : dow++;    break;
    case  6 : dow += 2; break;
    case  9 :
    case 12 : dow += 3; break;
    case  4 :
    case  7 : dow += 4; break;
    case  1 :
    case 10 : dow += 5; break;
    case  5 : dow += 6;
  }
  if (m > 2 || (y & 3)) dow++;
  return dow % 7;
}