Yanıtlar:
İstersiniz DateTime.DaysInMonth:
int days = DateTime.DaysInMonth(year, month);
Açıktır ki, Şubat bazen 28 ve bazen 29 gün olduğu için, yıllara göre değişir. Bir değere veya diğerine "sabitlemek" istiyorsanız, her zaman belirli bir yılı (sıçrama veya atlama) seçebilirsiniz.
Kod örneğinden System.DateTime.DaysInMonth kullanın :
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);
Bir aydaki günlerin sayısını bulmak için, DateTime sınıfı bir "DaysInMonth (int year, int month)" yöntemi sağlar. Bu yöntem, belirli bir aydaki toplam gün sayısını döndürür.
public int TotalNumberOfDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
VEYA
int days = DateTime.DaysInMonth(2018,05);
Çıkış: - 31
int days = DateTime.DaysInMonth(int year,int month);
veya
int days=System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(int year,int month);
yıl ve ayı geçmek zorundasınız, çünkü into zaman aydaki günler geçerli yıl ve ayda geri dönecektir
Seçilen ay ve yıl datetimepicker'dan aydaki günleri hesaplamasını sağladım ve ben ancak datetimepicker1 metnindeki kod, sonucu bu kodla bir metin kutusunda döndürmek için değiştirildi
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*/