【问题标题】:Display only the previous month's name in dropdown list (according to financial year)在下拉列表中仅显示上个月的名称(根据财政年度)
【发布时间】:2019-02-26 14:10:15
【问题描述】:

在下拉列表中,第 1 个月应从 4 月开始,到 3 月结束。 如果当前月份是 5 月,它应该只显示 4 月,同样,如果当前月份是 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();

上面是我的代码,它正在工作,但它从一月到十二月显示。

【问题讨论】:

  • 请在minimal reproducible example 上发布您为解决此问题所做的尝试。
  • 你是新的贡献者,请发布你的代码和什么不起作用。
  • @Gauravsa 我已经编辑了我的问题,请检查代码。
  • @CoolBots 我已经用代码更新了问题,请检查

标签: c# asp.net


【解决方案1】:

这行得通(我认为):

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();

【讨论】:

    【解决方案2】:

    类似这样的:

            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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多