代码之家  ›  专栏  ›  技术社区  ›  jlp

如何列出从过去日期到当前月份的月份和年份

  •  1
  • jlp  · 技术社区  · 15 年前

    我想有dropdownlist从过去的某个点到现在的一个月和几年。

    for (int year = 2009; year <= DateTime.Now.Year; year++)
    {
       for (int month = 1; month <= 12; month++)
       {
          DateTime date = new DateTime(year, month, 1);
          this.MonthsCombo.Items.Add(
              new RadComboBoxItem(date.ToString("Y"), 
                                  String.Format("{0};{1}", year, month))); // this is for reading selected value
       }
    }
    

    如何更改代码,使最后一个月成为当前月份?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Adriaan Stander    15 年前

    只有当市盈率低于今天时才增加价值。

    if (date <= DateTime.Today)
    {
        this.MonthsCombo.Items.Add( 
              new RadComboBoxItem(month.ToString("Y"),  
                                  String.Format("{0};{1}", year, m))); // this is for reading selected value 
    }
    

    或者,我会做一个while循环,比如

    DateTime startDate = new DateTime(2009, 01, 01);
    while (startDate <= DateTime.Today)
    {
    
        startDate = startDate.AddMonths(1);
    }