none
Generate a list of array for weeks and months for a given two dates between a specify Date Range

    Question

  • I have two dates, start and end dates and also Series End Date. I want to generate a weekly and monthly array of Dates for all specify period using Linq. So for example

    var start = DateTime.Parse("08/17/2018");
    var end = DateTime.Parse("09/17/2018");

    var SeriesEndPeriod = DateTime("12/31/2022")

    I want to generate array of all Dates for the start and end dates till the SeriesEndPeriod .Any help will be appreciated


    Ebenezer



    • Edited by denkyira Friday, February 8, 2019 5:32 PM
    Friday, February 8, 2019 5:31 PM

Answers

  • Check this (without LINQ):

    public class MonthStartEnd { public DateTime FirstDay; public DateTime LastDay; } . . . List<MonthStartEnd> months = new List<MonthStartEnd>(); DateTime s = StartDate.Day == 1 ? StartDate : StartDate.AddDays( -( StartDate.Day - 1 ) ).AddMonths( +1 );

    s = s.Date;

    for( ; ; ) { DateTime e = s.AddMonths( +1 ).AddDays( -1 ); if( e.Date > SeriesEndDate.Date ) break; months.Add( new MonthStartEnd { FirstDay = s, LastDay = e } ); s = s.AddMonths( +1 ); }



    • Edited by Viorel_MVP Tuesday, February 12, 2019 7:31 PM
    • Marked as answer by denkyira Wednesday, February 13, 2019 12:31 PM
    Tuesday, February 12, 2019 7:28 PM

All replies

  • Hi denkyira,

    Try the following statement and check if it works for you.

    var start = DateTime.Parse("08/17/2018");
    var end = DateTime.Parse("09/17/2018");
    var result =  Enumerable.Range(0, 1 + end.Subtract(start).Days)
                              .Select(offset => start.AddDays(offset))
                              .ToArray();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 12, 2019 6:08 AM
    Moderator
  • Hi Zhanglong

    Thanks a lot for your response. I have been able to achieve it for weekly by using the Linq code below

    //define helper class
    public class WeekStartEnd
    {
        public DateTime Monday;
        public DateTime Sunday;
    }
    
    //usage:
    DateTime StartDate = DateTime.Parse("02/12/2019");
    DateTime EndDate = DateTime.Parse("03/16/2019");
    DateTime SeriesEndDate= DateTime.Parse("12/31/2025");
    //find first monday
    DateTime firstMonday =  Enumerable.Range(0, 7)
        .SkipWhile(x => StartDate.AddDays(x).DayOfWeek != DayOfWeek.Monday)
        .Select(x => StartDate.AddDays(x))
        .First();
    //get count of days
    TimeSpan ts = (TimeSpan)(SeriesEndDate - firstMonday);
    //create new list of WeekStartEnd class
    List<WeekStartEnd> dates = new List<WeekStartEnd>();
    //add dates to list
    for(int i=0; i<ts.Days; i+=7)
    {
            //if(firstMonday.AddDays(i+6)<SeriesEndDate) //uncomment this line if you would like to get last sunday before SeriesEndDate
        dates.Add(new WeekStartEnd(){Monday=firstMonday.AddDays(i), Sunday=firstMonday.AddDays(i+6)});
    }

    Now I need it for monthly and since not all month is 30 days.


    Ebenezer

    Tuesday, February 12, 2019 5:12 PM
  • Check this (without LINQ):

    public class MonthStartEnd { public DateTime FirstDay; public DateTime LastDay; } . . . List<MonthStartEnd> months = new List<MonthStartEnd>(); DateTime s = StartDate.Day == 1 ? StartDate : StartDate.AddDays( -( StartDate.Day - 1 ) ).AddMonths( +1 );

    s = s.Date;

    for( ; ; ) { DateTime e = s.AddMonths( +1 ).AddDays( -1 ); if( e.Date > SeriesEndDate.Date ) break; months.Add( new MonthStartEnd { FirstDay = s, LastDay = e } ); s = s.AddMonths( +1 ); }



    • Edited by Viorel_MVP Tuesday, February 12, 2019 7:31 PM
    • Marked as answer by denkyira Wednesday, February 13, 2019 12:31 PM
    Tuesday, February 12, 2019 7:28 PM
  • Hi Viorel

    It works. Thanks a lot


    Ebenezer

    Wednesday, February 13, 2019 12:31 PM