How can I find the last day of the month in C#?
For example, if I have the date 03/08/1980, how do I get the last day of month 8 (in this case 31)?
How can I find the last day of the month in C#?
For example, if I have the date 03/08/1980, how do I get the last day of month 8 (in this case 31)?
From DateTimePicker:
First date:
DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
Last date:
DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));
To get last day of a month in a specific calendar - and in an extension method -:
public static int DaysInMonthBy(this DateTime src, Calendar calendar)
{
var year = calendar.GetYear(src); // year of src in your calendar
var month = calendar.GetMonth(src); // month of src in your calendar
var lastDay = calendar.GetDaysInMonth(year, month); // days in month means last day of that month in your calendar
return lastDay;
}