Q: How can I find the day of the week given the date?
A: Here are three methods:
#include <stdio.h> #include <time.h> char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; struct tm tm; tm.tm_mon = 2 - 1; tm.tm_mday = 29; tm.tm_year = 2000 - 1900; tm.tm_hour = tm.tm_min = tm.tm_sec = 0; tm.tm_isdst = -1; if(mktime(&tm) != -1) printf("%s\n", wday[tm.tm_wday]);When using mktime like this, it's usually important to set tm_isdst to -1, as shown (especially if tm_hour is 0), otherwise a daylight saving time correction could push the time past midnight into another day.
J is the number of the century [i.e. the year / 100], K the year within the century [i.e. the year % 100], m the month, q the day of the month, h the day of the week [where 1 is Sunday];
q + 26(m + 1) / 10 + K + K/4 + J/4 - 2J
h = (q + 26 * (m + 1) / 10 + K + K/4 + J/4 + 5*J) % 7;(where we use +5*J instead of -2*J to make sure that both operands of the modulus operator % are positive; this bias totalling 7*J will obviously not change the final value of h, modulo 7).
int dayofweek(int y, int m, int d) /* 0 = Sunday */ { 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; }
See also questions 13.14 and 20.32.
References:
ISO Sec. 7.12.2.3
Chr. Zeller, ``Kalender-Formeln''