C# 生成月份及天选择列表,方便做下拉框联动
月份及天选择列表,很方便做下拉框联动
/// <summary> /// 获取月份选择列表(根据当前语言环境显示月份名称) /// </summary> private IEnumerable<SelectListItem> GetMonthSelectList() { var dateFormat = DateTimeFormatInfo.GetInstance(Thread.CurrentThread.CurrentUICulture); for (var i = 1; i <= 12; 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, 1); var lastDayOfMonth = anyDate.AddMonths(1).AddDays(-1).Day; //该月最后一天 for (var d = 1; d <= lastDayOfMonth; d++) { yield return new SelectListItem { Text = d.ToString(), Value = d.ToString() }; } }