我想将日期范围分成相应的月份。 例如,我有以下方式的数据:
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));
}
}
}
答案 0 :(得分:2)
你正在用你的样本做这件事。找到月份的开始和结束有一种更简单的方法:
DateTime d = DateTime.Today;
DateTime monthStart = d.AddDays(-(d.Day - 1));
DateTime monthEnd = monthStart.AddMonths(1).AddDays(-1);
你真的不需要递归来做这个,顺便说一句,但是如果它是使用递归的赋值的一部分,那么只需创建一个采用开始和结束日期的函数。使用上面的方法查找月份开始和结束,按照您的意愿执行,然后将下个月的第一天(也称为monthEnd.AddDays(1)
)作为开始日期和原始结束日期传递回函数。 Voilà!递归。
答案 1 :(得分: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个列表:startings
和endings
[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行代码的快速脏代码可以实现这些值