Question 20.31

How can I find the day of the week given the date?


Use mktime or localtime (see questions 13.13 and 13.14, but beware of DST adjustments if tm_hour is 0), or Zeller's congruence (see the sci.math FAQ list), or this elegant code by Tomohiko Sakamoto:

dayofweek(y, m, d)	/* 0 = Sunday */
int y, m, d;		/* 1 <= m <= 12,  y > 1752 or so */
{
	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: ANSI Sec. 4.12.2.3
ISO Sec. 7.12.2.3


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback