Intro CS II/spring2020/cal

From Maryville College CS Wiki
Jump to: navigation, search
/**
   Computes the weekday of a given date.

   From Big C++ by Cay Horstmann
   @param year the year
   @param month the month (1 = January ... 12 = December)
   @param day the day of the month
   @return the weekday (0 = Sunday ... 6 = Saturday)
*/
int day_of_week(int year, int month, int day)
{
   int y = year; 
   int m = month - 1;

   if (month < 3) { y--; m = m + 4; }
   return (y + y / 4 - y / 100 + y / 400
      + 3 * m + 4 - (m - m / 8) / 2 + day) % 7;
}