我想将日期范围分成各个月份。例如,我有以下方式的数据:
startdate enddate
2013-04-04 2014-01-31
我希望能够以这种方式显示上述数据:
startdate enddate
2013-04-04 2013-04-30
2013-05-01 2013-06-30 --(2 month difference)
2013-07-01 2013-08-31
2013-09-01 2013-10-31
2013-11-01 2013-12-31
2014-01-01 2014-01-31
有人可以帮助我使用List
和递归吗?
没有递归尝试的示例代码
List<DateInformation> lDateCol1 = new List<DateInformation>();
List<DateInformation> lDateCol = new List<DateInformation>();
lResult.DateFrom = '4/4/2013';
lResult.DateUntil = '31/01/2014';
DateTime Today;
DateTime FirstDate = DateTime.MaxValue;
DateTime LastDate = DateTime.MinValue;
DateTime NextFirstDate = DateTime.MaxValue;
DateTime NextLastDate = DateTime.MinValue;
DateTime InputStartDate = DateTime.MaxValue;
DateTime InputEndDate = DateTime.MinValue;
Today = lResult.DateFrom;
FirstDate = new DateTime(Today.Year, 1, 1);
int monthsApart = Math.Abs(12 * (lResult.DateFrom.Year - lResult.DateUntil.Year) + lResult.DateFrom.Month - lResult.DateUntil.Month)
for (int daycount = 0; daycount < 12; daycount += noofmonths)
{
if (count == 0)
{
NextLastDate = FirstDate.AddMonths(noofmonths).AddDays(-1);
lDateCol1.Add(new DateInformation(FirstDate, NextLastDate));
}
if (count > 0)
{
NextFirstDate = NextLastDate.AddDays(1);
NextLastDate = new DateTime(NextFirstDate.Year, NextFirstDate.Month, 1).AddMonths(noofmonths).AddDays(-1);
lDateCol1.Add(new DateInformation(NextFirstDate, NextLastDate));
}
count = count + 1;
}
for (count = 0; count <= lDateCol1.Count - 1; count++)
{
if ((lDateCol1[count].DateFrom <= lResult.DateFrom) && (lResult.DateFrom <= (lDateCol1[count].DateUntil)))
{
InputStartDate = lResult.DateFrom;
InputEndDate = lDateCol1[count].DateUntil;
NextFirstDate = InputStartDate;
NextLastDate = InputEndDate;
lDateCol.Add(new DateInformation(NextFirstDate, NextLastDate));
break;
}
}
for (int daycount = 0; daycount <= monthsApart; daycount += noofmonths)
{
NextFirstDate = NextLastDate.AddDays(1);
NextLastDate = new DateTime(NextFirstDate.Year, NextFirstDate.Month, 1).AddMonths(noofmonths).AddDays(-1);
if (!(NextLastDate < lResult.DateUntil))
{
lDateCol.Add(new DateInformation(NextFirstDate, lResult.DateUntil));
break;
}
else
{
lDateCol.Add(new DateInformation(NextFirstDate, NextLastDate));
}
}
}