How to find the day of the week for a given date?

1.8k Views Asked by At

Please help me with my math problem

How to find the day of the week for a given date?

Give some simple solution or short cut for this problem

Thanks in advance

2

There are 2 best solutions below

3
On

You can find in out easily just by calculating the number of extra days after the complete number of weeks.

Suppose 1st January is Monday, then what day will 20th January be? Just see that there are 20 days from 1st January to 20th January. A complete week will have 7 days. So divide 20 by 7 and the remainder that you get is the number of extra days after the complete number of weeks. Here the remainder is 6. So there are 6 extra days. The first of these extra days will be Monday. The successive dates will be the successive days. So, that way 20th January will be Saturday.

With this method you can also find out the day of the week for a given date in a remote year. In that case, you need the fact that a normal year has 365 days while a leap year has 366 days. If you divide 365 by 7, then remainder is 1 and if you divide 366 by 7 the remainder is 2. Now, 1st June 2010 was Tuesday, then what day of the week was 2nd June 2013 ? Here, please note that 1st June 2010 to 30th May 2011 is a complete year. So, you get one extra day other than the complete weeks. Then, 1st June 2013 to 30th May 2012 is another complete year but 2012 is a leap year, so that you get two extra days now. Thus you have $1+2=3$ extra days. Next, 1st June 2012 to 30th May 2013 is another complete year and you get one more extra day. And up to 2nd June 2013 there will be two more extra days. Thus, from 1st June 2010 to 2nd June 2013 you have $1+2+1+2=6$ extra days other than the complete weeks. The first of these days will be Tuesday (because 1st June 2010 was Tuesday). Hence 2nd June 2013 was a Sunday.

0
On

I think this works. I have tested it quite carefully. I have gone through quite a number of code snippets for this function found online and found they didn't work :) Sorry about the bad formatting of the is_leap function; I can't work out how to do it right.

/*

  • Calculate day of week tm_wday from date.
  • Used to set the RTC from GPS, or from a user program which knows the date but not day of week.
  • DOW and DMY are in the same values as in the tm structure
  • This function is not fast but that doesn't matter because it gets called only when setting the RTC.
  • It works only for year 1900+.

*/

int day_of_week(int day, int month, int year) {

uint16_t mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
uint16_t i;

// Seed: 1 January 1900 is a Monday
uint32_t origin_day = 1;

// adjust for input values being from the standard tm structure, day 1.. year since 1900 etc
day--;
year += 1900;

 // add on how many days in previous years (since 1900)
for (i = 1900; i < year; i++)
{
    if (is_leap(i))
        origin_day += 366;
    else
        origin_day += 365;
}

// fix up Feb on this year in mtab (i = current year)
if (is_leap(i))
    mtab[1] = 29;

// add on how many days in whole preceeding months in the current year so far
// if month=0 (Jan) this is skipped
for (i=0; i<month; i++)
    origin_day += mtab[i];

// add on days in current month
origin_day += day;

// return value 0-6
return origin_day % 7;

}

/* A leap year is every year divisible by four except for years which are divisible by both 100 and 400 year is any year > 0 e.g. 2021 */

bool is_leap(uint16_t year) { if ( (year % 4) != 0 ) return false; if ( ( (year % 100) == 0) && ( (year % 400) != 0) ) return false; else return true; }