Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the days for particular month and year

Tags:

c#

.net

I have a method which passes two parameters Month and year

i will call this Method like this : MonthDates(January,2010)

public static string MonthDates(string MonthName,string YearName)
{
     return days;
}

How to get the days for particular month and year?

like image 801
user405477 Avatar asked Dec 01 '22 10:12

user405477


2 Answers

do you mean the number of days in a month?

System.DateTime.DaysInMonth(int year, int month)
like image 116
Andrew Bullock Avatar answered Dec 15 '22 03:12

Andrew Bullock


If you want all days as a collection of DateTime:

public static IEnumerable<DateTime> daysInMonth(int year, int month)
{
    DateTime day = new DateTime(year, month, 1);
    while (day.Month == month)
    {
        yield return day;
        day = day.AddDays(1);
    }
}

The use is:

IEnumerable<DateTime> days = daysInMonth(2010, 07);
like image 30
Kobi Avatar answered Dec 15 '22 02:12

Kobi