title: How to generate series with month span in C#
body: <p>I have a webservice which accepts the date range and due to huge data import i am passing the date range parameters for 1 month only.</p>
  
  <p>Acceptable start date format : 1-apr-2012</p>
  
  <p>Acceptable end date format :  30-apr-2012</p>
  
  <p>I want programatically send each month's start and end dates for last 2 years
  like 1-Jan-2012 till 31-aug-2012</p>
  
  <p>Below is sample piece of code which i started to test but its giving me span of 30 days when end date plus 1 month is added.</p>
  
  <pre><code>static void Main(string[] args)
      {
          string startDt = "1-apr-2011";
          string endDt = "30-apr-2012";
          DateTime dt = Convert.ToDateTime(startDt);
          DateTime dt2 = Convert.ToDateTime(endDt);
          CultureInfo culture = CultureInfo.GetCultureInfo("en-GB");
  
          //Console.WriteLine(value.ToString("D", culture)); 
  
          while (dt &lt; dt2)
          {
              Console.WriteLine(dt.ToString("D", culture));
             // Console.WriteLine(String.Format("{0:dd-MM-yyyy}", dt));
              dt = dt.AddMonths(1);                
          }
          Console.ReadLine();
      }
  </code></pre>
  
  <p><strong>Corrected Code:</strong></p>
  
  <pre><code> static void Main(string[] args)
      {
          string startDt = "1-apr-2011";
          string endDt = "30-apr-2012";
          DateTime dt = Convert.ToDateTime(startDt);
          DateTime dt2 = Convert.ToDateTime(endDt);
          CultureInfo culture = CultureInfo.GetCultureInfo("en-GB"); 
          while (dt &lt; dt2)
          {
              DateTime dtend = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month)); 
              Console.WriteLine(dt.ToString("D", culture));
              Console.WriteLine(dtend.ToString("D", culture));               
              dt = dt.AddMonths(1);                
          }
          Console.ReadLine();
      }
  </code></pre>