-5

我想将日期范围分成各个月份。例如,我有以下方式的数据:

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));
          }
        }
      }
4

2 回答 2

2

你正在用你的样本来做这件事。有一种更简单的方法可以找到月份的开始和结束:

DateTime d = DateTime.Today;
DateTime monthStart = d.AddDays(-(d.Day - 1));
DateTime monthEnd = monthStart.AddMonths(1).AddDays(-1);

顺便说一句,您实际上并不需要递归来执行此操作,但如果它是使用递归的分配的一部分,那么只需创建一个获取开始日期和结束日期的函数。使用上面的方法找到月份的开始和结束,对它们做你想做的事情,然后将下个月的第一天(又名monthEnd.AddDays(1))作为开始日期与原始结束日期一起传递回函数。瞧!递归。

于 2013-04-04T14:34:39.943 回答
0

刚刚做了一些快速的非递归代码

DateTime startdate = DateTime.Parse("2013-04-04");
DateTime enddate = DateTime.Parse("2014-01-31");
DateTime parseDate;
List<DateTime> startings = new List<DateTime>();
List<DateTime> endings = new List<DateTime>();
startings.Add(startdate);
parseDate = startdate.AddMonths(1);
while(parseDate.Day != 1)
    parseDate=parseDate.AddDays(-1);
parseDate=parseDate.AddDays(-1);
endings.Add(parseDate);
while (parseDate<enddate)
{
    parseDate = parseDate.AddDays(1);
    startings.Add(parseDate);
    parseDate = parseDate.AddMonths(2);
    parseDate = parseDate.AddDays(-1);
    endings.Add(parseDate);
}
endings[endings.Count() - 1] = enddate;

这保存了 2 个列表:startingsendings

[0] {04-04-2013 00:00:00}   System.DateTime
[1] {01-05-2013 00:00:00}   System.DateTime
[2] {01-07-2013 00:00:00}   System.DateTime
[3] {01-09-2013 00:00:00}   System.DateTime
[4] {01-11-2013 00:00:00}   System.DateTime
[5] {01-01-2014 00:00:00}   System.DateTime

[0] {30-04-2013 00:00:00}   System.DateTime
[1] {30-06-2013 00:00:00}   System.DateTime
[2] {31-08-2013 00:00:00}   System.DateTime
[3] {31-10-2013 00:00:00}   System.DateTime
[4] {31-12-2013 00:00:00}   System.DateTime
[5] {31-01-2014 00:00:00}   System.DateTime

如您所见,一些不到 20 行代码的快速脏代码可以实现这些值

于 2013-04-04T14:52:12.020 回答