Thread: Is there a funktion ?

  1. #1
    Registered User
    Join Date
    Nov 2003

    Talking Is there a funktion ?


    Im a Rookie and maybe you can/ want to help me out.

    Is there a funktion that can convert a date to the day ?

    I want the user to put in a date and the programm must give
    the day and if possible the week number.

    Im programming a console application. So I,m looking for a funktion in c++ standard libary.

    Thanks in advandge.

    And sorry for the bad englisch.

  2. #2
    Join Date
    Nov 2002
    Warwick, UK
    I think you'd need to create some kind of calendar... something like
    char January[31];
    January[0] = "Monday";
    January[1] = "Tuesday";
    // ...
    But I'm not sure about the rest.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    not that I know of... since it seems as if english isn't your first language, your probably not using US date format... so your format would be DD/MM/YYYY... just read it in like this:
    int day;
    int month;
    int year;
    char null;
    then you cold set an array like this:
    int calander[YEAR-2004][MONTH NUMBER-1][DAYS-1];
    then run a loop through putting a number representation of the day of the week, as in monday=1,tuesday=2, etc. I would put a limit on it, like no dates before last year or something... and no dates after 2010
    Last edited by major_small; 11-18-2003 at 10:39 AM.
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    There is nothing standard to c++ that will automatically calculate the day of the week for you, however you can calculate it mathematically yourself. Take the following into consideration:
    1996 = MON *
    1997 = WED
    1998 = THURS
    1999 = FRI
    2000 = SAT *
    2001 = MON
    2002 = TUES
    2003 = WED
    2004 = THURS *
    2005 = SAT
    2006 = SUN
    2007 = MON
    2008 = TUES *
    2009 = THURS
    2010 = FRI
    2011 = SAT
    2012 = SUN *
    (*=leap year)

    January 1 of year zero was a FRIDAY. Each January 1 the year after a leap year is two days of the week later and each year after that up to and including the next leap year is one day later.
    I just figured this out by looking at the day of the year each year and finding the pattern.

    Once you have the day of the week of the first day of the year, all you have to do is do the math to calculate the day of the week within that year. For example, january 30, 1996 would be done like so:
    1/1/1996 = MONDAY
    30 - 1 = 29 days later
    29 % 7 days a week = 1 day of the week later than 1/1/1996
    therefore, 1/30/1996 = TUESDAY

  5. #5
    Registered User
    Join Date
    Nov 2003
    Thank you all for this reply.

    greetings Gerard.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003


  7. #7
    Registered User
    Join Date
    May 2003
    Hi The following is a function for finding day of week I used in a college assignment

    In the main program you could use


    or input a date and parse into day, month, year

    //Function to find day name of date
       string dateName(int day, int month, int year)
       {//start of function dateName
         string dname[7]={"Sunday",
         int Y,M,D,d;//variables local to function
                      // array of day names  0 to 6
    		 if (month < 3)
           	 {//start of if
          		 Y = year - 1;
          		 M = month + 12;
          		 D = day;
           	 }//end of if
          	 {    //start of else
          		 Y = year;
          		 M = month;
          		 D = day;
          	 }   //end of else
           //formulae for finding day of week eg 0 to 6
          d = ((13 * M + 3) / 5 + D + Y + Y / 4 - Y / 100 + Y / 400 + 1) %7;
           // d = 0 to 6
    	  return dname[d];
       }//end of function dateName
    Regards William

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't use global variables or funktion parameters
    By cocobo in forum C Programming
    Replies: 8
    Last Post: 05-14-2005, 08:50 AM
  2. Funktion that returns the number of lines
    By overspray in forum C++ Programming
    Replies: 10
    Last Post: 10-04-2004, 11:00 PM
  3. creating a wait funktion whitch will....
    By Shogun in forum C Programming
    Replies: 3
    Last Post: 04-11-2003, 02:35 PM