在下拉列表中,第一个月应从四月开始,并于3月结束。如果本月可能只能显示四月,那么如果本月三月,则应动态列出(4月至2月)。
。如果本月是四月,则不应显示任何内容。
var previousMonth = DateTime.Now.Month == 1 ? 1 : DateTime.Now.Month - 1 ;
var months = Enumerable.Range(1, previousMonth).Select(i => new { I = i, M =
DateTimeFormatInfo.CurrentInfo.GetMonthName(i) });
DropDownList3.DataSource = months;
DropDownList3.DataTextField = "M";
DropDownList3.DataValueField = "I";
DropDownList3.DataBind();
上面是我的代码,但它显示为1月至12月。
这有效(我认为):
const int startMonth = 4;
var previousMonth = ((DateTime.Now.Month - 2) % 12) + 1;
var months = Enumerable.Range(startMonth - 1, (previousMonth - startMonth + 13) % 12).Select(i => new
{
I = (i % 12) + 1, M = DateTimeFormatInfo.CurrentInfo.GetMonthName((i % 12) + 1)
});
DropDownList3.DataSource = months;
DropDownList3.DataTextField = "M";
DropDownList3.DataValueField = "I";
DropDownList3.DataBind();
类似的东西:
List<string> months = new List<string>
{
"March",
"February",
"January",
"December",
"November",
"October",
"September",
"August",
"July",
"June",
"May",
"April",
""
};
var previousMonth = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
var newMonths = months.Skip(months.IndexOf(previousMonth)+1).Reverse().ToArray();
DropDownList2.DataSource = newMonths;
DropDownList2.DataTextField = "M";
DropDownList2.DataValueField = "I";
DropDownList2.DataBind();