C# 生成月份及天选择列表,方便做下拉框联动

时间:2024-10-31 11:34:02

月份及天选择列表,很方便做下拉框联动

/// <summary>
/// 获取月份选择列表(根据当前语言环境显示月份名称)
/// </summary>
private IEnumerable<SelectListItem> GetMonthSelectList()
{
var dateFormat = DateTimeFormatInfo.GetInstance(Thread.CurrentThread.CurrentUICulture);
for (var i = ; i <= ; i++)
{
yield return new SelectListItem { Text = dateFormat.GetMonthName(i), Value = i.ToString() };
}
}
/// <summary>
/// 获取指定月份的日选择列表
/// </summary>
private IEnumerable<SelectListItem> GetDaySelectList(int year, int month)
{
var anyDate = new DateTime(year, month, );
var lastDayOfMonth = anyDate.AddMonths().AddDays(-).Day; //该月最后一天
for (var d = ; d <= lastDayOfMonth; d++)
{
yield return new SelectListItem { Text = d.ToString(), Value = d.ToString() };
}
}