Risposte:
Vuoi DateTime.DaysInMonth:
int days = DateTime.DaysInMonth(year, month);
Ovviamente varia di anno in anno, dato che a volte febbraio ha 28 giorni e a volte 29. Puoi sempre scegliere un anno particolare (salta o meno) se vuoi "aggiustarlo" su un valore o su un altro.
Utilizzare System.DateTime.DaysInMonth , dall'esempio di codice:
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);
Per trovare il numero di giorni in un mese, la classe DateTime fornisce un metodo "DaysInMonth (int year, int month)". Questo metodo restituisce il numero totale di giorni in un mese specificato.
public int TotalNumberOfDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
O
int days = DateTime.DaysInMonth(2018,05);
Uscita: - 31
int days = DateTime.DaysInMonth(int year,int month);
o
int days=System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(int year,int month);
devi passare anno e mese poiché i intgiorni in mese saranno restituiti in base all'anno e al mese
L'ho fatto calcolare giorni nel mese dal datetimepicker selezionato mese e anno, e io ma il codice in datetimepicker1 ha cambiato il testo per restituire il risultato in una casella di testo con questo codice
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*/