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. how can i find number of days in a given year and month

how can i find number of days in a given year and month

Scheduled Pinned Locked Moved C#
question
19 Posts 6 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.
  • M musefan

    is 2100 really not a leap year? EDIT: It's OK I WIKI it and got an answer ;)

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

    A Offline
    A Offline
    Ahmed Khallaf
    wrote on last edited by
    #9

    i dont think

    1 Reply Last reply
    0
    • A Ahmed Khallaf

      how can i find number of days in a given year and month

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

      int daysInMonth = DateTime.DaysInMonth(2009, 2);
      Console.WriteLine(daysInMonth);

      A 1 Reply Last reply
      0
      • L Lost User

        int daysInMonth = DateTime.DaysInMonth(2009, 2);
        Console.WriteLine(daysInMonth);

        A Offline
        A Offline
        Ahmed Khallaf
        wrote on last edited by
        #11

        i don't know if the user will enter 2009 or 2008 or any month i want to take the data from the user

        modified on Tuesday, June 2, 2009 4:19 AM

        G 1 Reply Last reply
        0
        • A Ahmed Khallaf

          Many Thanks and i'm very happy from Your cooperation

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

          Ok this is what you should use instead...

          if(year % 4 == 0 && year % 100 == 0 ? year % 400 == 0 : true)//hope you get that logic
          //is leap year
          else
          //not leap year

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

          A G 2 Replies Last reply
          0
          • M musefan

            Ok this is what you should use instead...

            if(year % 4 == 0 && year % 100 == 0 ? year % 400 == 0 : true)//hope you get that logic
            //is leap year
            else
            //not leap year

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

            A Offline
            A Offline
            Ahmed Khallaf
            wrote on last edited by
            #13

            you realy have come to the right solution thank you

            1 Reply Last reply
            0
            • A Ahmed Khallaf

              i don't know if the user will enter 2009 or 2008 or any month i want to take the data from the user

              modified on Tuesday, June 2, 2009 4:19 AM

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #14

              Then you just use variables in the call to the DaysInMonth method instead of the literal values used in the example.

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • M musefan

                Ok this is what you should use instead...

                if(year % 4 == 0 && year % 100 == 0 ? year % 400 == 0 : true)//hope you get that logic
                //is leap year
                else
                //not leap year

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

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #15

                Or if you don't want to reinvent the wheel:

                if (DateTime.IsLeapYear(year)) {
                // is leap year
                } else {
                // not leap year
                }

                ;)

                Despite everything, the person most likely to be fooling you next is yourself.

                M 1 Reply Last reply
                0
                • G Guffa

                  Or if you don't want to reinvent the wheel:

                  if (DateTime.IsLeapYear(year)) {
                  // is leap year
                  } else {
                  // not leap year
                  }

                  ;)

                  Despite everything, the person most likely to be fooling you next is yourself.

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

                  I think we all need new wheels :)

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

                  1 Reply Last reply
                  0
                  • A Ahmed Khallaf

                    how can i find number of days in a given year and month

                    A Offline
                    A Offline
                    Ahmed Khallaf
                    wrote on last edited by
                    #17

                    i finished simple code for this topic and i want you to review it sory for not optimized code but see the idea download the project: http://www.geocities.com/ahmed_117/Month.zip[^]

                    G 1 Reply Last reply
                    0
                    • A Ahmed Khallaf

                      i finished simple code for this topic and i want you to review it sory for not optimized code but see the idea download the project: http://www.geocities.com/ahmed_117/Month.zip[^]

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #18

                      Have a look at this:

                      private void button1_Click(object sender, EventArgs e) {
                      button1.Enabled = false;
                      int year = dateTimePicker1.Value.Year;
                      int month = dateTimePicker1.Value.Month;
                      DateTime start = new DateTime(year, month, 1);
                      dateTimePicker1.Value = start;
                      int days = DateTime.DaysInMonth(year, month);
                      for (int i = 0; i < days; i++) {
                      dataGridView1.Rows.Add();
                      dataGridView1.Rows[i].Cells[0].Value = start.AddDays(i).DayOfWeek;
                      dataGridView1.Rows[i].Cells[1].Value = start.AddDays(i).ToShortDateString();
                      }
                      }

                      Despite everything, the person most likely to be fooling you next is yourself.

                      A 1 Reply Last reply
                      0
                      • G Guffa

                        Have a look at this:

                        private void button1_Click(object sender, EventArgs e) {
                        button1.Enabled = false;
                        int year = dateTimePicker1.Value.Year;
                        int month = dateTimePicker1.Value.Month;
                        DateTime start = new DateTime(year, month, 1);
                        dateTimePicker1.Value = start;
                        int days = DateTime.DaysInMonth(year, month);
                        for (int i = 0; i < days; i++) {
                        dataGridView1.Rows.Add();
                        dataGridView1.Rows[i].Cells[0].Value = start.AddDays(i).DayOfWeek;
                        dataGridView1.Rows[i].Cells[1].Value = start.AddDays(i).ToShortDateString();
                        }
                        }

                        Despite everything, the person most likely to be fooling you next is yourself.

                        A Offline
                        A Offline
                        Ahmed Khallaf
                        wrote on last edited by
                        #19

                        a very Clever man this what i want to reach thank you

                        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