How to get days for a specific month and year

I have a method that passes two parameters Month and year

I will call this method as follows: MonthDates (January 2010)

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

How to get days for a specific month and year?

+4
source share
4 answers

Do you mean the number of days in a month?

 System.DateTime.DaysInMonth(int year, int month) 
+12
source

If you want all days to be in a DateTime collection:

 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 following is used:

 IEnumerable<DateTime> days = daysInMonth(2010, 07); 
+5
source
 System.DateTime.Now.Month System.DateTime.Now.Year System.DateTime.Now.Day 

And so on ......... You have a lot of things you can get from DateTime.Now

+2
source

instead of a string, try to declare an enumeration as follows

 public enum Month { January = 1, February, March, .... so on } 

then pass it to the function and use the following functions in your function.

 return System.DateTime.DaysInMonth(year, month); 

Instead of a string, try using an integer, as this will reduce the overhead of syntax strings.

+1
source

All Articles