1

Possible Duplicate:
how to get the days for particular month and year

Given the following method:

GetDaysPerMonth(string monthName, string year);

And the values "1" and "2010", how can I return the number of days within the specified month?

Community
  • 1
  • 1
user405477
  • 51
  • 1
  • 4

3 Answers3

7

How about using DateTime.DaysInMonth?

System.DateTime.DaysInMonth(year, month)

As an aside though, and possibly opening myself up to be shouted at, couldn't you just have tried typing "get days in month" into Google?

Noldorin
  • 144,213
  • 56
  • 264
  • 302
Rob Levine
  • 40,328
  • 13
  • 85
  • 111
  • 1
    +1 for the google comment. http://www.google.com/#hl=en&source=hp&q=get+number+of+days+in+month+c%23&btnG=Google+Search&aq=f&aqi=&aql=&oq=get+number+of+days+in+month+c%23&gs_rfai=&fp=b8ea99853242201d the search gives your specified api as 5th result (in msdn). – Shaihi Jul 29 '10 at 10:25
  • 1
    The OP didn't specify the calander system they're using, so Tim's answer is better. – Skizz Jul 29 '10 at 10:27
  • @Skizz - yes. There is an implicit assumption in mine that user wants the days in the month for the currently set calendar, which may not actually be true. – Rob Levine Jul 29 '10 at 10:35
6

Calendar.GetDaysInMonth

Note that the Calendar class accepts month numbers, not month names, because month names are vary in different languages.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
2

You simply want the System.DateTime.DaysInMonth static method.

Example:

var daysInMonth = DateTime.DaysInMonth(2010, 7) // = 31

If you're given the year and month as strings, just call int.Parse to get the value as a number.

Noldorin
  • 144,213
  • 56
  • 264
  • 302