Determing the day for a given date

This is a discussion on Determing the day for a given date within the C++ Programming forums, part of the General Programming Boards category; i want to determine the day (sunday, monday, etc..) of a given date (ex: august 3rd, 2005). i have written ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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)
    Last edited by misplaced; 10-10-2004 at 12:46 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Not sure if this is an option, but you could use the microsoft calendar control if you only needed this to run on windows.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Misplaced, instead of using the for loop just do:
    Code:
    daytracker=((daytracker+2)%7)+1;
    Last edited by PJYelton; 10-10-2004 at 02:31 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    wait a second..................

    that's not right....

    that above code you gave is
    dayTracker = ((3+2) % 7) + 1;
    dayTracker = ((dayTracker+2) % 7) + 1;

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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;
                    
        
    }

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  12. #12
    Banned
    Join Date
    Aug 2004
    Posts
    128
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 09:55 AM
  2. Getting the Following Date
    By BB18 in forum C Programming
    Replies: 2
    Last Post: 10-10-2004, 01:39 PM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21