/* calculates the name of the weekday for the 1st day of a given month & year Michael Sanders 2024 https://busybox.neocities.org/notes/get-first-day-of-month.txt Further reading... http://datagenetics.com/blog/november12019/index.html https://www.themathdoctors.org/zellers-rule-what-day-of-the-week-is-it/ https://en.wikipedia.org/wiki/Zeller%27s_congruence */ #include const char *weekdays[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; int getFirstDayOfMonth(int year, int month) { /* Zeller's Congruence Algorithm calculates the name of the weekday for the 1st day of the month (Sun, Mon, etc) note: months are indexed 1 to 12 */ if (month < 3) { month += 12; year -= 1; } int K = year % 100; int J = year / 100; int d = 1; // 1st day of the month int f = d + 13 * (month + 1) / 5 + K + K / 4 + J / 4 + 5 * J; return (f % 7) - 1; } int main(void) { const char *FirstDayOfAugust = weekdays[getFirstDayOfMonth(2024, 8)]; printf("First day of August 2024: %s\n", FirstDayOfAugust); return 0; } // eof