Autor Beitrag
ots_sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mi 22.10.14 11:27 
Hi,

ich möchte eine Listbox mit einem Monatswert und Jahr füllen (October 2014). Dazu habe ich ein Startdatum und ein Enddatum. Irgenwie stehe ich total auf dem Schlauch und finde nicht die richtige Lösung. Kann hier bitte jemand Unterstützen, vielen Dank.

Stefan


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
DateTime FirstMonth = new DateTime(2013,10,01);
string fm = String.Format("{0:yyyy-mm-dd}", FirstMonth);          
DateTime ThisMonth = DateTime.Now;
string tm = String.Format("{0:yyyy-mm-dd}", ThisMonth);
int monthDiff = 1;

while (tm > fm)
{
  lb.item.add("mm yyyy")
}
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4701
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 22.10.14 11:46 
Falls du doch lieber das Datumsformat wie in deinem Code und nicht wie in deinem Text möchtest dann solltest du noch mm durch MM ersetzen. mm bezieht sich auf die Minuten nicht Monate.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
private void DeineLiebeMethode()

    DateTime firstMonth = new DateTime(2013,10,01);
    DateTime thisMonth = DateTime.Now;

    deineLiebeListBox.DataSource = GetListOfMonthes(firstMonth, thisMonth).Select(x => String.Format("{0:MMMM yyyy}", x)).ToList();
}

private IEnumerable<DateTime> GetListOfMonthes(DateTime start, DateTime end)
{
    var month = new DateTime(start.Year, start.Month, 1000, start.Kind);
    var endMonth = new DateTime(end.Year, end.Month, 1000, end.Kind);

    while (month <= endMonth) 
    {
        yield return month;
        month = month.AddMonths(1);   
    }
}

Für diesen Beitrag haben gedankt: ots_sharp