how to get the days for particular month and year

后端 未结 4 792
天涯浪人
天涯浪人 2021-01-13 17:37

I have a method which passes two parameters Month and year

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

public static string MonthDa         


        
4条回答
  •  情深已故
    2021-01-13 18:15

    If you want all days as a collection of DateTime:

    public static IEnumerable 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 days = daysInMonth(2010, 07);
    

提交回复
热议问题