-1

how i can get dates ranges list between 2 dates

for example,

start date is 12 February end date is 23 May

so, the result must be like

12-Feb to 28-Feb 1-Mar to 31-Mar 1-April to 30-April 1-May to 23-May

Ghalib Ali
  • 11
  • 4
  • 4
    Possible duplicate of [How do I loop through a date range?](https://stackoverflow.com/questions/1847580/how-do-i-loop-through-a-date-range) – Emre Savcı Apr 08 '18 at 08:27
  • 2
    why does February become Feb but April stays April? – Francesco B. Apr 08 '18 at 08:30
  • Do you need to loop over these ranges or do you just need a list with the dates required? – Steve Apr 08 '18 at 08:31
  • Basically you create DateTime for first day and add one day till reaching last day. Use AddDays. Google for a snipet. – derloopkat Apr 08 '18 at 08:36
  • loop over ranges – Ghalib Ali Apr 08 '18 at 08:38
  • It's not clear what your comment of "loop over ranges" is meant to indicate, but it sounds like you probably just want to consider three parts to the result: 1) the month that includes the start; 2) the months entirely between the start and end; 3) the month that includes the end. You may want to handle "start and end are in the same month" as a special case for simplicity. – Jon Skeet Apr 08 '18 at 08:40

1 Answers1

1

Let's suppose you are checking only one year, i.e. the current one (2018). Of course you can make StartDate and EndDate parametric.

There's nothing special in the code below; I used "d-MMMM" as date format since you seemed to ask for the minimum number of days ("d" does that, i.e. selects 1 and not 01 for the first day of a month) and the "long" name of the month ("MMMM" does that, check this answer). If instead you just want the abbreviation, i.e. "Feb" for "February", you can use "MMM" (check this other answer).

DateTime StartDate = new DateTime(2018, 2, 12);
DateTime EndDate = new DateTime(2018, 5, 23);

for (int i = 0; i <= EndDate.Month - StartDate.Month; i++)
{
    DateTime FirstDay = new DateTime(2018, StartDate.Month + i, 1);
    if (i == 0)
    {
        FirstDay = StartDate;
    }
    DateTime LastDay = new DateTime(2018, StartDate.Month + i, DateTime.DaysInMonth(2018, FirstDay.Month));
    if (i == EndDate.Month - StartDate.Month)
    {
        LastDay = EndDate;
    }
    Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}

Console.ReadLine();

You can also use the following code to make it "cross-year":

static void Main(string[] args)
{
    PrintDateList(new DateTime(2018, 2, 12), new DateTime(2018, 5, 23));
    PrintDateList(new DateTime(2018, 11, 12), new DateTime(2019, 2, 23));

    Console.ReadLine();
}

private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
    Console.WriteLine("");
    int TotalMonths = EndDate.Month - StartDate.Month +
                     (EndDate.Year - StartDate.Year) * 12;

    int CurrentYear = StartDate.Year;
    int MonthsToSubtract = 0;

    for (int i = 0; i <= TotalMonths; i++)
    {
        int CurrentMonth = StartDate.Month + i;

        if (StartDate.Month + i > 12)
        {
            if ((StartDate.Month + i) % 12 == 1)
            {
                CurrentYear++;
                Console.WriteLine(CurrentYear.ToString());
                MonthsToSubtract = StartDate.Month + i - 1;
            }

            CurrentMonth = StartDate.Month + i - MonthsToSubtract;
        }

        DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
        if (i == 0)
        {
            FirstDay = StartDate;
        }
        DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
        if (i == TotalMonths)
        {
            LastDay = EndDate;
        }
        Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
    }
}

The output is

12-February to 28-February
1-March to 31-March
1-April to 30-April
1-May to 23-May

12-November to 30-November
1-December to 31-December
1-January to 31-January
1-February to 23-February

Since you asked, I replaced Console.WriteLine with a List:

private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
        List<Tuple<DateTime>> EventsDatesRangeList = new List<Tuple<DateTime>>();
        Console.WriteLine("");
        int TotalMonths = EndDate.Month - StartDate.Month +
                        (EndDate.Year - StartDate.Year) * 12;

        int CurrentYear = StartDate.Year;
        int MonthsToSubtract = 0;

        for (int i = 0; i <= TotalMonths; i++)
        {
            int CurrentMonth = StartDate.Month + i;

            if (StartDate.Month + i > 12)
            {
                if ((StartDate.Month + i) % 12 == 1)
                {
                    CurrentYear++;
                    Console.WriteLine(CurrentYear.ToString());
                    MonthsToSubtract = StartDate.Month + i - 1;
                }

                CurrentMonth = StartDate.Month + i - MonthsToSubtract;
            }

            DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
            if (i == 0)
            {
                FirstDay = StartDate;
            }
            DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
            if (i == TotalMonths)
            {
                LastDay = EndDate;
            }
                Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
            EventsDatesRangeList.Add(new Tuple<DateTime>(FirstDay));
            EventsDatesRangeList.Add(new Tuple<DateTime>(LastDay));

        }
    }

enter image description here

Francesco B.
  • 2,729
  • 4
  • 25
  • 37