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.
  • A Ahmed Khallaf

    thank you for your replay and you solved the problem and i remembered that if we want to know if February in a leap year or not by divided the year by 4 and if the result is an integer number it will be leap year such as 2004/4=501 this is a leap year and 2005/4=501.25 it's not a leap year and i'll to try it by code Many thanks for all

    modified on Tuesday, June 2, 2009 3:48 AM

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #4

    Ooh - not quite. If it's a century, it has to be divisible by 400 to be a leap year, so 2000 is a leap year and 2100 isn't.

    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

    My blog | My articles | MoXAML PowerToys | Onyx

    R M 2 Replies Last reply
    0
    • A Ahmed Khallaf

      thank you for your replay and you solved the problem and i remembered that if we want to know if February in a leap year or not by divided the year by 4 and if the result is an integer number it will be leap year such as 2004/4=501 this is a leap year and 2005/4=501.25 it's not a leap year and i'll to try it by code Many thanks for all

      modified on Tuesday, June 2, 2009 3:48 AM

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

      use modulus (provides the remainder after a division) not divide...

      if(year % 4 == 0)
      //is leap year
      else
      //not leap year

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

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Ooh - not quite. If it's a century, it has to be divisible by 400 to be a leap year, so 2000 is a leap year and 2100 isn't.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #6

        Let us hope that his code will not have to run for a century. :)

        It is a crappy thing, but it's life -^ Carlo Pallini

        1 Reply Last reply
        0
        • P Pete OHanlon

          Ooh - not quite. If it's a century, it has to be divisible by 400 to be a leap year, so 2000 is a leap year and 2100 isn't.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

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

          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 1 Reply Last reply
          0
          • M musefan

            use modulus (provides the remainder after a division) not divide...

            if(year % 4 == 0)
            //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
            #8

            Many Thanks and i'm very happy from Your cooperation

            M 1 Reply Last reply
            0
            • 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