title: Calculating months between 2 dates, then calculating days in each month
body: <p>So I'm trying to create a graph by getting the start and end date from the user. I need to count the number of months between the dates, then for each month calculate the number of days and get the name of the month as well. </p>
  
  <p>I've got the number of months between the 2 dates but can't seem to translate the remaining into code. I have a class called MonthRange</p>
  
  <pre><code>public class MonthRange
  {
      public DateTime startDate { get; set; }
  
      public DateTime endDate { get; set; }
  
      public string monthName { get; set; }
  
      public MonthRange() {
      }
  
      public MonthRange(DateTime startDate, DateTime endDate, string monthName) {
          this.startDate = startDate;
          this.endDate = endDate;
          this.monthName = monthName;
      }
  }
  </code></pre>
  
  <p>And my method:</p>
  
  <pre><code>private List&lt;MonthRange&gt; GetRangeOfMonthsBetweenDates(DateTime startDate, DateTime endDate) {
  
       List&lt;MonthRange&gt; result = new List&lt;MonthRange&gt;();
       int months = (endDate.Year - startDate.Year)*12 + endDate.Month - startDate.month;
  
  foreach(var m in months){
       //get the start and end date of that month with the name and add it to the result list.
  }
  
  }
  </code></pre>
  
  <p>The kicker is that if the startDate is midway through the month, then that is what should be saved in the result list, likewise if the endDate is midway of the month, that is what should be saved. I'm a bit lost and would appreciate any help. </p>
  
  <p>Edit: So this is an example of what I'm trying to achieve. I'm trying to create a graph that plots the number of times a person has had food in a given time range. </p>
  
  <p>Edit 2: So i ended up going this way:</p>
  
  <pre><code> private List&lt;MonthRange&gt; GetRangeOfMonthsBetweenDates(DateTime startDate, DateTime endDate) {
  
          List&lt;MonthRange&gt; result = new List&lt;MonthRange&gt;();
  
          DateTime holder = startDate;
  
          var months = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month;
  
  
          for (int i = 0; i &lt;= months; i++) {
              if (i == 0)
              {
                  result.Add(new MonthRange(startDate, CalculateStartOfMonth(endDate), startDate.ToString("MMM")));
              }
              else if (i == months)
              {
                  result.Add(new MonthRange(CalculateEndOfMonth(startDate), endDate, endDate.ToString("MMM")));
              }
              else {
                  DateTime middleMonth = holder.AddMonths(1);
                  result.Add(new MonthRange(CalculateStartOfMonth(middleMonth), CalculateEndOfMonth(middleMonth), middleMonth.ToString("MMM")));
              }
          }
  
          return result;
      }
  
      private DateTime CalculateStartOfMonth(DateTime endDate) {
          var startOfMonth = new DateTime(endDate.Year, endDate.Month, 1);
          return startOfMonth;
      }
  
      private DateTime CalculateEndOfMonth(DateTime startDate)
      {
          var endOfMonth = new DateTime(startDate.Year, startDate.Month, DateTime.DaysInMonth(startDate.Year, startDate.Month));
          return endOfMonth;
      }
  </code></pre>