0

I have a start Date : 01-01-2019 and End-Date 31-12-2019

I need to get all possible periods between those dates, but by interval.

Interval can be 180, 90, 45, 15 , 7 and 1 days.

So if I select 180 days return should be a list with those periods:

  1. 01/01/2019 - 30/06/2019 (First period of generated list )

  2. 01/07/2019 - 31/12/2019 (Second period of generated list )

It is almost like Generate list of list for start and end date of week from date range But I need do this in C#

How to find a list of Dates from a Start-Date and End-Date Is not quite What I need. But is close.

DateTime startDate = DateTime.Parse("01/01/2019");
DateTime endDate = DateTime.Parse("31/12/2019");

dayInterval = 45;             

startDate = startDate.AddDays(dayInterval);
endDate = endDate.AddDays(-dayInterval);

I also tried:

var startDate = new DateTime(2019, 1, 1);
var endDate = new DateTime(2019, 12, 31);
int days = 45;

List<DateTime> range = Enumerable.Range(0, days)
    .Select(i => startDate.AddDays(i))
    .ToList();
thiagovinicius
  • 117
  • 1
  • 12
  • 1
    1/6/2019 - 1/1/2019 = 151 days. Your example is wrong – Steve Jul 19 '19 at 09:33
  • Guyz hit the flag as dupe. Dupe are not a bad thing. They act as road sign, showing everyone a clear path to the solution. Dupe are not a bad thing, saying the same question in other word will help everyone with the same word find the question. If you have any New answer please post in on the dupe. Centralize all the answer on the target. – xdtTransform Jul 19 '19 at 09:51
  • And my dupe target comment is gone Thnk community…Dear gold hammer and power user ear my pray: Here is the dupe target [Split date range into date range chunks](https://stackoverflow.com/questions/13690617/split-date-range-into-date-range-chunks) – xdtTransform Jul 19 '19 at 09:52

2 Answers2

0

Maybe try write that code like this:

tatic IEnumerable<DateTime> GetAllDatesAndInitializeTickets(DateTime startDate, DateTime endDate)
{
    List<DateTime> allDates = new List<DateTime>();


    for (DateTime i = startDate; i <= endDate; i = i.AddDays(45))
    {
        allDates.Add(i);
    }
    return allDates.AsReadOnly();
}
Prochu1991
  • 443
  • 5
  • 20
0

The solution proposed here is what I need Split date range into date range chunks

public static IEnumerable<Tuple<DateTime, DateTime>> SplitDateRange(DateTime start, DateTime end, int dayChunkSize)
    {
        DateTime chunkEnd;
        while ((chunkEnd = start.AddDays(dayChunkSize)) < end)
        {
            yield return Tuple.Create(start, chunkEnd);
            start = chunkEnd;
        }
        yield return Tuple.Create(start, end);
    }
thiagovinicius
  • 117
  • 1
  • 12