954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Day of week given a date

By vegaseat on Oct 9th, 2004 9:10 am

So you want to find out which day of the week you were born. Well at least some of us do. I actually wrote this program because my whole family was born on Sundays, and my friends didn't believe it! An oldie but goodie, moved from DeSmet C to Turbo C and now to Pelles C. I just had somebody tell me that 01/01/1800 was a Wednesday, check it out with this program.

// function to return the day of the week given the date
// (01/01/1800 was supposed to be a Wednesday)
// original Turbo C, modified for Pelles C by  vegaseat    8oct2004
// Pelles C free at:  http://smorgasbordet.com/pellesc/index.htm

#include <stdio.h>  // for printf(), scanf(), getchar()

// years ending with 00 have to be divisible by 400 to leap
// note the "&&" is a DaniWeb problem and should be a double & for AND
#define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))

int isdatevalid(int month, int day, int year);
int weekday(int month, int day, int year);

char week[7][10] = {
  "Monday","Tuesday","Wednesday","Thursday",
  "Friday","Saturday","Sunday"
};

int main()
{
  int  month, day, year;
  
  printf("Return the day of the week given the date.");
  printf("Enter date in the form mm/dd/yyyy : ");
  scanf("%d/%d/%d",&month,&day,&year);
  if (isdatevalid(month,day,year))
  {
    printf("The day of the week for this date is %s",
      week[weekday(month,day,year)]);
  }
  else
    printf("%d/%d/%d not a valid date!",
    month,day,year);
  
  getchar();   // wait 
  getchar();   // 2nd wait needed
  return 0;
}

//
//   return 1 if date is valid, 0 otherwise.
//
int isdatevalid(int month, int day, int year)
{
  if (day <= 0) return 0 ;
  switch( month )
	{
	  case 1  :
	  case 3  :
	  case 5  :
	  case 7  :
	  case 8  :
 	  case 10 :
	  case 12 : if (day > 31) return 0 ; else return 1 ;
	  case 4  :
	  case 6  :
	  case 9  :
	  case 11 : if (day > 30) return 0 ; else return 1 ;
	  case 2  : 
	    if ( day > 29 ) return 0 ;
      if ( day < 29 ) return 1 ;
      if (isleapyear(year)) return 1 ;   // leap year
    else return 0 ;
	}
  return 0 ;
}

//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// tested for 1901 to 2099 (seems to work from 1800 on too)
// 
int weekday(int month, int day, int year)
{	
  int ix, tx, vx;

  switch (month) {
    case 2  :
	case 6  : vx = 0; break;
	case 8  : vx = 4; break;
	case 10 : vx = 8; break;
	case 9  :
	case 12 : vx = 12; break;
	case 3  :
	case 11 : vx = 16; break;
	case 1  :
	case 5  : vx = 20; break;
	case 4  :
	case 7  : vx = 24; break;
  }
  if (year > 1900)  // 1900 was not a leap year
    year -= 1900;
  ix = ((year - 21) % 28) + vx + (month > 2);  // take care of February 
  tx = (ix + (ix / 4)) % 7 + day;              // take care of leap year
  return (tx % 7);
}

main returns int, and you should validate the input somewhere or all hell could break loose. Good idea though, and a nice start.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thats a good.......

indranil87
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

HI DUDE I DID THIS IN JAVA

[CODE=JAVA]
import java.util.*;
class c
{
public static void main(String args[])

{

float ix, tx,aa,v=0;

int w;

System.out.println("KNOW THE DAY WHEN DATE IS PROVIDED IN REQUESTED FORM");
System.out.println("----------------------------------------------------");
Scanner scanner = new Scanner(System.in);
System.out.println("ENTER THE DAY NOTE:PLEASE DONT ADD '0' IF THE DAY IS SINGLE DIGIT LIKE 1 TO 9");
int day = scanner.nextInt();
System.out.println("ENTER THE MONTH NOTE:PLEASE DONT ADD '0' IF THE MONTH IS SINGLE DIGIT LIKE 1 TO 9");
int month = scanner.nextInt();
System.out.println("ENTER THE YEAR");
int year = scanner.nextInt();

if (day <= 0){
System.out.print("ON WHICH PLANET DAYS ARE LESS THAN 0 ??");
System.exit(0);
}

switch( month )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : if (day > 31){ System.out.println("invalid date"); System.exit(0);}
case 4 :
case 6 :
case 9 :
case 11 : if (day > 30) { System.out.println("invalid date"); System.exit(0);}
case 2 : if ( day > 29 ) { System.out.println("invalid date"); System.exit(0);}
if ( day < 29 )
System.out.println("valid date");
}


switch (month) {
case 2 :
case 6 : v = 0; break;
case 8 : v = 4; break;
case 10 : v = 8; break;
case 9 :
case 12 : v = 12; break;
case 3 :
case 11 : v = 16; break;
case 1 :
case 5 : v = 20; break;
case 4 :
case 7 : v= 24; break;
}

if (year > 1900) // 1900 was not a leap year
year -= 1900;

ix = ((year - 21) % 28);
ix=ix+v;
if(month > 2);
{ix=ix + month;} // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
aa=(tx % 7);
w=(int)aa;

if(w==0)
{System.out.println("ITS A MONDAY");
System.exit(0);}
if(w==1){
System.out.println("ITS A TUESDAY");
System.exit(0);}
if(w==2){
System.out.println("ITS A WEDNESDAY");
System.exit(0);}
if(w==3){
System.out.println("ITS A THURSDAY");
System.exit(0);
}
if(w==4){
System.out.println("ITS A FRIDAY");
System.exit(0);
}
if(w==5){
System.out.println("ITS A SATURDAY");
System.exit(0);}
if(w==6)
{
System.out.println("ITS A SUNDAY");
System.exit(0);}

}}
[\CODE]

THIS ONES ONLY WORKING FOR 7TH MONTH IS ANY THING WRONG WITH MY CODE IF YES PLEASE . . .PLEASE. . . ASSIST. . .I LL BE ONLINE FOR ONE MORE HOUR

aashishkotagiri
Newbie Poster
1 post since Jul 2009
Reputation Points: 5
Solved Threads: 0
 

excuse me... can you explain the int weekday(int month, int day, int year) fuction at the buttom part?
i can't understand the purpose of the variables ix, tx, and vx
and the if statement at the very bottom... pls,,,help!

statistician
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You