Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Day of the Week

Day of the Week

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob

    D L F 4 Replies Last reply
    0
    • B BobInNJ

      Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Are you using MFC?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

      1 Reply Last reply
      0
      • B BobInNJ

        Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Something like the following, with maybe a tweak or three:

        struct tm time_in = { 0, 0, 0, 1, 7, 2013 - 1900 }; // August 1st, 2013

        time_t time_out = mktime(&time_in);

        struct tm *time_local = localtime(&time_out);

        _tprintf(_T("%u\n"), time_local->tm_wday);

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

        1 Reply Last reply
        0
        • B BobInNJ

          Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          See http://msdn.microsoft.com/en-us/library/by5d3kb1(v=vs.90).aspx[^]

          D 1 Reply Last reply
          0
          • L Lost User

            See http://msdn.microsoft.com/en-us/library/by5d3kb1(v=vs.90).aspx[^]

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Nice idea, but the OP was looking for something portable.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            1 Reply Last reply
            0
            • B BobInNJ

              Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob

              F Offline
              F Offline
              Freak30
              wrote on last edited by
              #6

              I would suggest converting the date to a Julian Day Number. Here is a function we use to convert day, month and year to such a number. The input values (D, M, Y) are all unsigned int.

              unsigned int yh = Y;
              unsigned long c, ya, j;
              if (M > 2)
              {
              M -= 3; }else{
              M += 9;
              Y--;
              }
              c = Y / 100;
              ya = Y - 100*c;
              j = ((146097*c)>>2) + ((1461*ya)>>2) + (153*M + 2)/5 + D;

              Using that you just need the Julian Day Number corresponding to a date for which you know the day of week as a reference. You can then subtract the numbers to get the days in between. Please note that the function is only valid from 14th September 1752 (when the Gregorian calendar started).

              The good thing about pessimism is, that you are always either right or pleasently surprised.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups