我有一个包含所有月份的组合框.
我需要知道的是所选月份的天数.
var month = cmbMonth.SelectedIndex + 1;
DateTime date = Convert.ToDateTime(month);
Run Code Online (Sandbox Code Playgroud)
因此,如果用户选择1月,我需要将31保存到变量.谢谢.
Jon*_*eet 270
int days = DateTime.DaysInMonth(year, month);
Run Code Online (Sandbox Code Playgroud)
显然它会随着年份而变化,因为有时2月有28天,有时候是29天.如果你想把它"固定"到一个值或其他值,你总是可以选择特定年份(跳跃与否).
Pet*_*ron 30
从代码示例中使用System.DateTime.DaysInMonth:
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);
Run Code Online (Sandbox Code Playgroud)
小智 9
要查找一个月中的天数,DateTime类提供了一个方法“DaysInMonth(int year, int month)”。 此方法返回指定月份的总天数。
public int TotalNumberOfDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
Run Code Online (Sandbox Code Playgroud)
或者
int days = DateTime.DaysInMonth(2018,05);
Run Code Online (Sandbox Code Playgroud)
输出:- 31
| 归档时间: |
|
| 查看次数: |
109954 次 |
| 最近记录: |