Respuestas:
Tu quieres DateTime.DaysInMonth:
int days = DateTime.DaysInMonth(year, month);
Obviamente, varía según el año, ya que a veces febrero tiene 28 días y otras 29. Siempre puede elegir un año en particular (salto o no) si desea "fijarlo" a un valor u otro.
Use System.DateTime.DaysInMonth , del ejemplo de código:
const int July = 7;
const int Feb = 2;
// daysInJuly gets 31.
int daysInJuly = System.DateTime.DaysInMonth(2001, July);
// daysInFeb gets 28 because the year 1998 was not a leap year.
int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);
// daysInFebLeap gets 29 because the year 1996 was a leap year.
int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);
Para encontrar el número de días en un mes, la clase DateTime proporciona un método "DaysInMonth (int year, int month)". Este método devuelve el número total de días en un mes específico.
public int TotalNumberOfDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
O
int days = DateTime.DaysInMonth(2018,05);
Salida: - 31
int days = DateTime.DaysInMonth(int year,int month);
o
int days=System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(int year,int month);
tiene que pasar año y mes, ya que los intdías del mes volverán al año y mes en curso
Hice que calcule días en el mes a partir de datetimepicker seleccionado mes y año, y yo pero el código en datetimepicker1 textchanged para devolver el resultado en un cuadro de texto con este código
private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
int s = System.DateTime.DaysInMonth(DateTimePicker1.Value.Date.Year, DateTimePicker1.Value.Date.Month);
TextBox1.Text = s.ToString();
}
int month = Convert.ToInt32(ddlMonth.SelectedValue);/*Store month Value From page*/
int year = Convert.ToInt32(txtYear.Value);/*Store Year Value From page*/
int days = System.DateTime.DaysInMonth(year, month); /*this will store no. of days for month, year that we store*/