DIGIT-ARC read-only archive · snapshot: July 23, 2026

any idea to find weekdays and weekends for given month and year?

14 posts · originally from https://geek.digit.in/community/threads/any-idea-to-find-weekdays-and-weekends-for-given-month-and-year.73739/

hrushij
hrushij Original post #1
Nov 21, 2007 at 10:14 AM
Hello friends
I have a problem with a C++ Program...
I have to find weekdays and weekends for a goven month and year!

Ant Idea??

Thanx in Advance..
harryneopotter
Nov 21, 2007 at 10:23 AM
i made a prog long ago to find the day fr a given date/month/year ....... i can pm u if it can be of some help. Coz it will be better if u make the prog ... we r here only to guide. I can give u direction only ........
fun2sh
fun2sh #3
Nov 21, 2007 at 11:34 AM
Hint: a calender is repeated after every six years. so takin a month n year as a reference u can calculate within those six years n then u can see for the given years falls under which category.
for example-- take nov 2007 as refrence n calculate for next six years(ie till nov of 2013). if given years is now 2015 then it wil hav same calender as year of 2009
harryneopotter
Nov 21, 2007 at 11:43 AM
no need to do that ......... i have a formula to calculate the day for any date ..
so if u want to knw weekends and weekdays for any month... just use it to get the day for the 1st of that month/year and calculate for the whole month.
fun2sh
fun2sh #5
Nov 21, 2007 at 12:02 PM
Then Y Dont U Atleast Post The Formula!!!
harryneopotter
Nov 21, 2007 at 12:11 PM
Code:
#include<conio.h>
#include<stdio.h>
void main()
{int dy,mo,yr,day=0;
clrscr();
printf("Enter your Date of Birth:");
printf("\nDate:");
scanf("%d",&dy);
printf("Month:");
scanf("%d",&mo);
printf("Year:");
scanf("%d",&yr);
if(yr>1999)
yr--;
yr=yr%100;
day=dy+yr;


day=day+((yr-1)/4);


switch(mo)
{case 1: day--;
       break;
 case 2:day=day+2;
	break;
 case 3:day=day+3;
	break;
 case 4:day=day+6;
	break;
case 5:day=day+8;
	break;
case 6:day=day+11;
	break;
case 7:day=day+13;
	break;
case 8:day=day+16;
	break;
case 9:day=day+19;
	break;
case 10:day=day+21;
	break;
case 11:day=day+24;
	break;
case 12:day=day+26;
	break;
		  }
		  if(yr%4==0)
		  day++;

		  

	day=day%7;

	switch(day)
	{
	case 0:printf("Sunday");
	       break;
	case 1:printf("Monday");
	       break;
	case 2:printf("Tuesday");
	       break;
	case 3:printf("Wednesday");
	       break;
	case 4:printf("Thursday");
	       break;
	case 5:printf("Friday");
	       break;
	case 6:printf("Saturday");
	       break;
		  }







getch();
}

But it works only for dates after the year 1900..... check with ur b'date
Pathik
Pathik #7
Nov 21, 2007 at 12:18 PM
@fun2sh normally every year the Day increases by 365%52 =1 for every date.. But wen a leap year comes the day increases by 1+1 for every date.. So wat did u mean by that 6 yr thing?? @harry pls post the formula
fun2sh
fun2sh #8
Nov 21, 2007 at 12:43 PM
wil u explain me the logic for the above program??

pathiks said:
@fun2sh normally every year the Day increases by 365%52 =1 for every date.. But wen a leap year comes the day increases by 1+1 for every date.. So wat did u mean by that 6 yr thing?? @harry pls post the formula
i mean that calender is repeated after every 6 years. see calender of nov 2012 or nov 2001

no i checked the calenders again its repeatin after 11 years.
harryneopotter
Nov 21, 2007 at 1:01 PM
lets say u want to find the day for 15th August 1947

1. take the last two digits of the year i.e. 47
2. Add the date in the resulting no i.e. 47+15= 62
3. add the no of leap years before that year in that century (means after 1900) which comes here to be 11...i.e. 62+11= 73
4. No add the no of days in that year before that month ( i have used a simplified methd of this in the prog ) which is 212 in this case i.e. 73+212= 285
5. Now divide this no by 7 and note the remainder
285%7 = 5
Now if remainder is 0 then its Sunday
1 then its Monday
2 then its Tuesday
3 then its Wednesday
4 then its Thursday
5 then its Friday
6 then its Saturday


6. If the date is after 1st jan 2000 then reduce 1 from the year before adding.


To sum up
{ year(yy) + no of leap years before that year + no of days in that year till the desired date ( date + days before that month ) } divide by 7 and check the remainder ................ for years b/w 1900-1999
after that subtract 1 from the year .......... thts it.

hope u got it .

ohh haan ... forgot to tell ....... 15th aug 1947 was on FRIDAY ....
S
sakumar79 #10
Nov 21, 2007 at 1:37 PM
Check out *everything2.com/index.pl?node_id=1023392 for another possible solution - there is an explanation and also a C code listed...

Arun
fun2sh
fun2sh #11
Nov 21, 2007 at 1:37 PM
thnx
hrushij
hrushij #12
Nov 22, 2007 at 7:34 AM
Yes Of course we can use the day from the date formula...but is there any other solution for it....
I dont think that at every 6 year calender is always same,..bcs
we have leap year on every 4 years and at century it id not there..

Is it???
By the way thx for these........everything
QwertyManiac
Nov 22, 2007 at 8:12 AM
Won't using a formula be the most efficient solution to the problem? Why would you want an alternative solution?
harryneopotter
Nov 22, 2007 at 1:19 PM
offtopic :
Qwerty, is it the THUNDERCATS logo ?