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#
  4. Calculate How Many Mondays in a Particular Month

Calculate How Many Mondays in a Particular Month

Scheduled Pinned Locked Moved C#
helpquestion
22 Posts 8 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.
  • C cdpace

    Hello guys Can you please help me with this one? I want to make a mehtod that when you pass it a particular month and year it will return you the ammount of Mondays that are found in that Month of that particular day. Anyone has any ideas? Thank you in adavance Regards, Christian Pace

    M Offline
    M Offline
    musefan
    wrote on last edited by
    #9

    Here's a method you could use...

    int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
    {
    DateTime dt = new DateTime(year, month, 1);
    int result = 0;
    for(int i = 0; i < DateTime.DaysInMonth(year, month); i++)
    {
    if(dt.DayOfWeek == dayToCount)
    result++;
    dt.AddDays(1);
    }
    return result;
    }

    Life goes very fast. Tomorrow, today is already yesterday.

    M 2 Replies Last reply
    0
    • M musefan

      Here's a method you could use...

      int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
      {
      DateTime dt = new DateTime(year, month, 1);
      int result = 0;
      for(int i = 0; i < DateTime.DaysInMonth(year, month); i++)
      {
      if(dt.DayOfWeek == dayToCount)
      result++;
      dt.AddDays(1);
      }
      return result;
      }

      Life goes very fast. Tomorrow, today is already yesterday.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #10

      Improved...

      int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
      {
      DateTime dt = new DateTime(year, month, 1);
      int firstInstance = 1;
      if((int)dt.DayOfWeek > (int)dayToCount)
      firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
      else if((int)dt.DayOfWeek < (int)dayToCount)
      firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
      return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
      }

      ...I think the logic is right. [EDIT] That was way off, I fixed now... I think!

      Life goes very fast. Tomorrow, today is already yesterday.

      L P 2 Replies Last reply
      0
      • M musefan

        Improved...

        int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
        {
        DateTime dt = new DateTime(year, month, 1);
        int firstInstance = 1;
        if((int)dt.DayOfWeek > (int)dayToCount)
        firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
        else if((int)dt.DayOfWeek < (int)dayToCount)
        firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
        return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
        }

        ...I think the logic is right. [EDIT] That was way off, I fixed now... I think!

        Life goes very fast. Tomorrow, today is already yesterday.

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

        I'm not too sure about the various constants, but it looks like The Right Way to do it

        M L 2 Replies Last reply
        0
        • N Nagy Vilmos

          NSS


          Panic, Chaos, Destruction. My work here is done.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #12

          Not everyone picks up on these things.

          1 Reply Last reply
          0
          • L Lost User

            I'm not too sure about the various constants, but it looks like The Right Way to do it

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #13

            On second thoughts, it does not look much improved :laugh: The constants are OK, they will never need to change

            Life goes very fast. Tomorrow, today is already yesterday.

            1 Reply Last reply
            0
            • M musefan

              Here's a method you could use...

              int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
              {
              DateTime dt = new DateTime(year, month, 1);
              int result = 0;
              for(int i = 0; i < DateTime.DaysInMonth(year, month); i++)
              {
              if(dt.DayOfWeek == dayToCount)
              result++;
              dt.AddDays(1);
              }
              return result;
              }

              Life goes very fast. Tomorrow, today is already yesterday.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #14

              What??? It does not work? What's with the 'Bad Answer' again?

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • C cdpace

                Hello guys Can you please help me with this one? I want to make a mehtod that when you pass it a particular month and year it will return you the ammount of Mondays that are found in that Month of that particular day. Anyone has any ideas? Thank you in adavance Regards, Christian Pace

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #15

                Obviously you do not need any loop or conditional for this. Have a look at:

                int mondays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 6).DayOfWeek) / 7;

                What remains of this homework is for you to figure out why the expression is what it is! :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                P 1 Reply Last reply
                0
                • L Lost User

                  I'm not too sure about the various constants, but it looks like The Right Way to do it

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #16

                  harold aptroot wrote:

                  it looks like The Right Way to do it

                  really? :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  L 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    harold aptroot wrote:

                    it looks like The Right Way to do it

                    really? :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


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

                    *reconsiders* Yes? No seriously, no matter how wrong it was, not having a loop made it much closer than all those other entries..

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Obviously you do not need any loop or conditional for this. Have a look at:

                      int mondays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 6).DayOfWeek) / 7;

                      What remains of this homework is for you to figure out why the expression is what it is! :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #18

                      No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.

                      L 2 Replies Last reply
                      0
                      • P PIEBALDconsult

                        No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #19

                        IMO it works for all years and months, I checked for this year only. With a day-of-week parameter it becomes:

                        int Xdays=(DateTime.DaysInMonth(year, month) + (int)new DateTime(year, month, 7-dayOfWeek).DayOfWeek) / 7;

                        where dayOfWeek is 0=SU, 1=MO, ... 6=SA [ADDED] so you can also use (int)DayOfWeek [/ADDED] :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                        modified on Wednesday, November 18, 2009 3:36 PM

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          No, it's obvious that no loop is required, but I still have two conditionals in mine. I, like some of the others, am not content to hard code Monday, but would prefer to take the day of the week as a parameter. I haven't tried yours yet, but I don't see how it can be a general solution for any specified day of the week. On the whole, I expect the OP's teacher only wants to prove that he's smarter than his students.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #20

                          PIEBALDconsult wrote:

                          I expect the OP's teacher only wants to prove that he's smarter than his students

                          The teacher may be shaded then by a cheating student. :laugh:

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                          C 1 Reply Last reply
                          0
                          • M musefan

                            Improved...

                            int CountDaysInMonth(int month, int year, DayOfWeek dayToCount)
                            {
                            DateTime dt = new DateTime(year, month, 1);
                            int firstInstance = 1;
                            if((int)dt.DayOfWeek > (int)dayToCount)
                            firstInstance += (7 - (int)dt.DayOfWeek) + (int)dayToCount;
                            else if((int)dt.DayOfWeek < (int)dayToCount)
                            firstInstance += (int)dayToCount - (int)dt.DayOfWeek;
                            return firstInstance <= (DateTime.DaysInMonth(month, year) - 28) ? 5 : 4;
                            }

                            ...I think the logic is right. [EDIT] That was way off, I fixed now... I think!

                            Life goes very fast. Tomorrow, today is already yesterday.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #21

                            What I came up with is similar to that:

                            public static int
                            F
                            (
                            int Year
                            ,
                            int Month
                            ,
                            System.DayOfWeek DayOfWeek
                            )
                            {
                            int d = (int) DayOfWeek - (int) (new System.DateTime ( Year , Month , 1 )).DayOfWeek ;

                            if ( d < 0 )
                            {
                                d += 7 ;
                            }
                            
                            int n = System.DateTime.DaysInMonth ( Year , Month ) - 28 ;
                            
                            return ( d < n ? 5 : 4 ) ;
                            

                            }

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              PIEBALDconsult wrote:

                              I expect the OP's teacher only wants to prove that he's smarter than his students

                              The teacher may be shaded then by a cheating student. :laugh:

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                              C Offline
                              C Offline
                              cdpace
                              wrote on last edited by
                              #22

                              Lol guys Thank you very much for the help ;) but actually this ain't for homework hehe, this is part of a payroll system that I am developing at work and with the deadline stress and from being tired because been programming 8 hours strait i couldn't find a solution for this so i turned to some help ;) hehe thank you again guys.

                              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