# Determing the day for a given date

• 10-09-2004
misplaced
Determing the day for a given date
i want to determine the day (sunday, monday, etc..) of a given date (ex: august 3rd, 2005). i have written a function which count the number of days into the year that the date is...example, feb 1st is 32 days into the year. i can't figure out how to do this though... i know that jan 1st, 1900 was a tuesday, but i really have no idea what to do with that . any suggestions?

also, i posted the code below because it seemed too easy and produced the right output the very first time.... any insight on possible errors which might occur in special cases? month, day, and year will always have valid values
Code:

```//declared in date.h int Date::month; int Date::day; int Date::year; //dummy function until i confirm it's accuracy bool Date::isLeap(void) {     return !(year % 4); }   int Date::daysIntoYear(void) {         if(month == 1)                 return day;         int count = 31;         int lastMonth = 31;                for(int i = 2; i < month; i++)         {                 if(lastMonth == 31)                  //if the last month had                 {                                      //31 days, this month                                                           //has 30 days                         if(i != 8)                                      ///<--unless this month                                              {                                //is august                                  lastMonth = 30;                         }                         count += lastMonth;                 }else{                         lastMonth = 31;                         count += lastMonth;                 }         }         if(month > 2)                              //if past Feb, remove         {                                        //1 or 2 days (if leap 1, else 2)                 if(isLeap()){ count--; }else{ count -= 2; }         }         return (count + day);         }```
• 10-09-2004
misplaced
i know that i could do:
Code:

```yearDiff = int((year - 1900) * 365.25) + daysIntoYear(this->date); //this ^^ is the number of days between jan 1, 1900 and the given date dayTracker = 3;          //jan 1, 1900 is a tuesday sunday =1, monday = 2, tuesday = 3, etc.. for(int i = 0; i < yearDiff; i++) {     if(dayTracker == 7)  //7 = saturday             dayTracker = 1;     else             dayTracker++; } return dayTracker;```

but that seems rather ineffient

(i know there's some special cases where that will return the wrong value, i cma e up with that off the top of my head)
• 10-10-2004
bithub
Not sure if this is an option, but you could use the microsoft calendar control if you only needed this to run on windows.
• 10-10-2004
The Brain
I'd have to do my homework on this one.. these are some things i would consider though

1.) I think there would have to be a 'base year'.. kinda like a 'control' in which you can reference.. starting new years based on this control

2.) I'm thinking an enumerated list perhaps.. if i wanted to know how many days in each month

3.) I'm also thinking about incrementing February every 4 years
• 10-10-2004
Salem
• 10-10-2004
PJYelton
Misplaced, instead of using the for loop just do:
Code:

`daytracker=((daytracker+2)%7)+1;`
• 10-10-2004
misplaced
that was actually my original idea...but it didn't seem to be right....
int yearDiff = year - REF_YEAR; //ref_year = 1900
long daysSince = long(yearDiff * 365.25) + daysIntoYear();
return ((daysSince - REF_DAY )% 7) + REF_DAY; //REF_DAY = 3

where did you get the +2 and the +1?....

and does this logic make sense for my purpose, or did you just see that my loop could be compacted without regard to it's context? in other words, is this going to work right?
• 10-10-2004
misplaced
wait a second..................

that's not right....

that above code you gave is
dayTracker = ((3+2) % 7) + 1;
dayTracker = ((dayTracker+2) % 7) + 1;
• 10-10-2004
PJYelton
No, its right. Watch, January 4th, 1900 is a Saturday. Daytracker should equal 3.

daytracker=((3+2)%7)+1;
daytracker=(5%7)+1;
daytracker=5+1=6=Saturday.

The +2 comes from the fact that the first day is a Tuesday which is two days after the first day of the week. The +1 comes from the fact that you have the days numbered 1-7 instead of 0-6 and modulus returns 0-6. You might want to change to number the days as 0-6 to make array use easier, for example if you want to do something like this:
Code:

```string days[7]={"Sunday", "Monday", etc etc}; daytracker=0; cout<<days[daytracker]<<endl;  \\ prints "Sunday"```
I can't think of an easier way to calculate the day than with this formula, and in my opinion is pretty straight forward and efficient.
• 10-10-2004
misplaced
ok...i still don't see what you mean....but i figured it out on my own....kind of

this following code gives me the right results (only if the year is a leap year....i see the problem here)....

what my problem is why does it work?
Code:

``` int Date::dayName(void) {     int yearDiff = year - REF_YEAR;         //i know there is a an error here because of the leap     //this is why i only get correct results when it is a leap year     long daysSince = long(yearDiff * 365.25) + daysIntoYear();             if(daysSince % 7)         return daysSince % 7;            //why does this work? i didn't tell 'it' anywhere that                                                         //jan 1 1900 is a tuesday     else         return 7;                     }```
• 10-10-2004
PJYelton
I don't think it does work even for leap years. For example, January 1st 1996 is a Monday. Your formula gives:

yeardiff=196
daySince=196*365.25+1=71590
71590%7=1 which is Sunday.

I just noticed that your DaySince() function doesn't return the day like I thought it did, I thought it subtracted the number of days since January 1, 1900 but its really figuring out the number of days since December 31, 1899. For example, I would think January 2nd, 1900 is one day since January 1st, 1900 but your function would return 2. So with your formula day zero is actually a Monday, not a Tuesday. If instead of your numbering system we make Sunday=0, Monday=1, Tuesday=2 etc, the formula would be:
Code:

`dayOfWeek=(daysSince%7+REF_DAY);  // REF_DAY==1==MONDAY`
If you want to keep your numbering of days the way it is, just add 1 afterwards.
• 10-10-2004
JarJarBinks
Here is an example from cplusplus.com

Code:

```/* mktime example: weekday calculator */ #include <stdio.h> #include <time.h> int main () {   time_t rawtime;   struct tm * timeinfo;   int year, month ,day;   char * weekday[] = { "Sunday", "Monday",                       "Tuesday", "Wednesday",                       "Thursday", "Friday", "Saturday"};   /* prompt user for date */   printf ("Enter year: "); scanf ("%d",&year);   printf ("Enter month: "); scanf ("%d",&month);   printf ("Enter day: "); scanf ("%d",&day);   /* get current timeinfo and modify it to user's choice */   time ( &rawtime );   timeinfo = localtime ( &rawtime );   timeinfo->tm_year = year - 1900;   timeinfo->tm_mon = month - 1;   timeinfo->tm_mday = day;   /* call mktime: timeinfo->tm_wday will be set */   mktime ( timeinfo );   printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);     return 0; }```