1,105,538 Community Members

Day of week given a date

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
 
3
 

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);
}

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

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

Member Avatar
indranil87
Newbie Poster
1 post since Jul 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thats a good.......

Member Avatar
statistician
Newbie Poster
1 post since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
RounaqJJW
Newbie Poster
7 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

can't we use a string function pls?

Member Avatar
deceptikon
Eternally Awesome
4,690 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

can't we use a string function pls?

Um...what?

Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
0
 

can't we use a string function pls?

Do'et.

Member Avatar
yde
Newbie Poster
3 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This is a very nostalgic problem for me.
Several years ago (so long ago that Dani was probably not born yet) I had to solve this problem. I did all sorts of pre-internet researc but could not find an answer.

Then - the aha moment

I realized that there is no information in a single date that can tell you what day of the week it fell on but if you do know the day of the week for a certain day (e.g. today) you can

  1. Calculate the difference in days between your known date and the date in question.
  2. Find the modulo 7 difference of that difference.
  3. Since the days of the week continuously repeat in a 7 day cycle, the modulo 7 value will determine the day of the week you want.

There are 2 caveats.

  1. Leap years (i.e. an extra day) occur every four years EXCEPT if the year is 0 mod 400 (e.g. 2000 was not a leap year).
  2. Several centuries ago (I don't recall when) a few months were dropped from the calendar (by papal decree?) to bring it back into synch with the seasons.

I wrote code to do this calculation but I can't find it, so I'll leave implementation as
"an exercise for the reader". Have fun!

Member Avatar
deceptikon
Eternally Awesome
4,690 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
1
 

I wrote code to do this calculation but I can't find it, so I'll leave implementation as "an exercise for the reader". Have fun!

Piece of cake considering I've already written this for my standard C library implementation. Here's the meat of it, slightly modified:

#include <stdio.h>

#define _LEAP_YEAR(year)  (((year) > 0) && !((year) % 4) && (((year) % 100) || !((year) % 400)))
#define _LEAP_COUNT(year) ((((year) - 1) / 4) - (((year) - 1) / 100) + (((year) - 1) / 400))

const int yeardays[2][13] = {
    { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
    { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }
};

const int monthdays[2][13] = {
    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};

int weekday(int year, int month, int day)
{
    int ydays, mdays, base_dow;

    /* Correct out of range months by shifting them into range (in the same year) */
    month = (month < 1) ? 1 : month;
    month = (month > 12) ? 12 : month;

    mdays = monthdays[_LEAP_YEAR(year)][month - 1];

    /* Correct out of range days by shifting them into range (in the same month) */
    day = (day < 1) ? 1 : day;
    day = (day > mdays) ? mdays : day;

    /* Find the number of days up to the requested date */
    ydays = yeardays[_LEAP_YEAR(year)][month - 1] + day;

    /* Find the day of the week for January 1st */
    base_dow = (year * 365 + _LEAP_COUNT(year)) % 7;

    return (base_dow + ydays) % 7;
}

Dates are tricky though, and this only works within the confines of ISO C library requirements.

Member Avatar
vijayan121
Posting Virtuoso
1,769 posts since Dec 2006
Reputation Points: 1,097 [?]
Q&As Helped to Solve: 329 [?]
Skill Endorsements: 16 [?]
 
0
 

can't we use a string function pls?

Not functions in <string>, but a couple of functions in <ctime>

#include <iostream>
#include <ctime>

int main()
{
    int  month, day, year ;
    std::cin >> month >> day >> year ;
    // validate

    std::tm tm ;
    tm.tm_mon = month - 1 ;
    tm.tm_mday = day ;
    tm.tm_year = year - 1900 ;
    tm.tm_hour = tm.tm_min = tm.tm_sec = 0 ;
    tm.tm_isdst = -1 ;

    static const char* const wdays[] =
    { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday" } ;

    std::time_t t = std::mktime( &tm ) ;
    const std::tm* ptm = std::localtime( &t ) ;

    if( ptm ) std::cout << wdays[ ptm->tm_wday ] << '\n' ;
    else std::cout << "invalid date\n" ;
}
Member Avatar
YrthWyndAndFyre
Newbie Poster
10 posts since Jul 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

For those of you interested, there is a Python version here ...
http://www.daniweb.com/software-development/python/code/447745/day-of-week-given-a-date#

Member Avatar
jnawrocki
Light Poster
49 posts since Aug 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 
// On what day were you born?
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>

using namespace std; 

long jd(int y, int m, int d);

void main() 
{
    int Year, Month, Day, DayOfWeek;
    long Julian;

    Year = 2001;  // If only one time doing might I hard code the date
    Month = 10;
    Day = 02;

    Julian = jd(Year, Month, Day); // Convert the date to Julian
    DayOfWeek = Julian % 7         // The remainder should give us the day of week  
    switch(DayOWeek)
    {

        case 0:
            cout << "Your birthday occurred on Monday"
            break;
        case 1:
            cout << "Your birthday occurred on Tuesday"
            break;
        case 2:
            cout << "Your birthday occurred on Wednesday"
            break;
        case 3:
            cout << "Your birthday occurred on Thursday"
            break;
        case 4:
            cout << "Your birthday occurred on Friday"
            break;
        case 5:
            cout << "Your birthday occurred on Saturday"
            break;
        case 6:
            cout << "Your birthday occurred on Sunday"
            break;
        default:
            cout << " You must not have been born"
            break
    }
}

// Convert date to julian
 long jd(int y, int m, int d)
 {
    y+=8000;
    if(m<3) { y--; m+=12; }
     return (y*365) +(y/4) -(y/100) +(y/400) -1200820 +(m*153+3)/5-92 +d-1;
 }
You
Post:
Start New Discussion
Tags Related to this Article