English 中文(简体)
按月/年分列的人口总数
原标题:populate drop down list with month/year
  • 时间:2011-01-22 03:06:01
  •  标签:
  • c#
  • asp.net

我有一天,我需要从那天到今天的几个月/年人口减少。 例如,如果这一日期是10/14/2010,那么下降幅度应当包含2010年10月、2010年11月、2010年12月、2011年1月。

我对这样做的想法是,从今天的倒退到一个月,把每个月的收集工作增加到这个日期,最后交还一套扼杀。 接着,对上页负荷实行下调控制。 最后,使用一些具有页数方法的jax来平息铺设,并引发部分页数的重载。

我很想知道这样做是否容易。

感谢。

最佳回答
public static List<string> GetMonths(DateTime StartDate)
  {
   List<string> MonthList = new List<string>();
   DateTime ThisMonth = DateTime.Now.Date;

   while (ThisMonth.Date > StartDate.Date)
   {
    MonthList.Add(ThisMonth.ToString("MMMM") + " " + ThisMonth.Year.ToString());
    ThisMonth = ThisMonth.AddMonths(-1);
   }

   return MonthList;
  }
问题回答

Maybe you can try this:

static IEnumerable<DateTime> monthsBetween(DateTime startDate, DateTime endDate)
    {
        return Enumerable.Range(0, (endDate.Year - startDate.Year) * 12 + (endDate.Month - startDate.Month + 1))
                         .Select(m => new DateTime(startDate.Year, startDate.Month, 1).AddMonths(m));
    }

这不会给你造成你想要的准确格式,而是让你们流出。

你可以做这样的事情,这是你所描述的,但展望未来:

private string[] FillDropDownWithDates(DateTime dt)   
{
        DateTime dtnow = DateTime.Now;

        List<string> values =  new List<string>();

        if ( (dt <= dtnow))
        {
            values.Add(String.Format("{0:y}", dt));
        }
        while ( (dt = dt.AddMonths(1)) <= dtnow || ( dt.Month == dtnow.Month && dt.Year == dtnow.Year) )
        {                
            values.Add(String.Format("{0:y}", dt));  // "March, 2008"                     YearMonth
        }


        return values.ToArray();

    }

For Year,

public static IEnumerable<int> Range (int start, int count)
{
    int end = start + count;

    for (int i = start; i < end; i++) 
        yield return i;
}

var startYear = 2000;
YearDropDownList.ItemsSource= Enumerable.Range(startYear, 2050 - startYear + 1);

For Month, An enumerable list with .ToString("MMMM") format.

这就是我如何用12个月/年。 希望守则有助于。

public IEnumerable<SelectListItem> Additional12Months {
    get
    {
       return Enumerable.Range(12, 12).Select(i => new SelectListItem { Value = DateTime.Now.AddMonths(-(i)).ToShortDateString(), Text = DateTime.Now.AddMonths(-(i)).ToString("MMM-yyyy") }).ToList();                
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签