title: Get weeks from start and end date in javascript
body: <p>I am building a timetable function, for that I have to insert the data week wise in an collection.</p>
  
  <p>So I'm creating a array which will contain the weeks from start_date to end_date. </p>
  
  <p><strong>1st Push in an array:</strong> start_date = Declared date (if declared date is Sunday, then it consider the date of coming Monday); end_date = date on Saturday</p>
  
  <p><strong>2nd till nth Push in an array:</strong> start_date = date on Monday; end_date = date on Saturday or declared end date if its within the week</p>
  
  <pre><code>var start = new Date("09/30/2016");
  var end = new Date("11/2/2016");
  
  var count = 0;
  var sDate;
  var eDate;
  var dateArr = [];
  
  while(start &lt;= end){
      if (start.getDay() == 0){
          count = 0;
      }else {
          if(count == 0){
              sDate = start;
              count = 1
          }else {
              count = 1;
          }
  
          if(start.getDay() == 6 || start == end){
              count = 1
              eDate = start;
          }else {
              count = 1;
          }
  
          if(sDate &amp;&amp; eDate){
              sDate = new Date(sDate)
              eDate = new Date(eDate)
              dateArr.push({'startDate': sDate, 'endDate': eDate});
              sDate = undefined;
              eDate = undefined;
          }
      }
  
      var newDate = start.setDate(start.getDate() + 1);
      start = new Date(newDate);
  }
  </code></pre>
  
  <p>But the result Im getting is this</p>
  
  <pre><code>[{
      'startDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Tue Oct 04 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Tue Oct 11 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Tue Oct 18 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Tue Oct 25 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST),
  }]
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p><strong>Expected Result:</strong></p>
  
  <pre><code>[{
      'startDate':Fri Sep 30 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Mon Oct 03 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Mon Oct 10 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Mon Oct 17 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Mon Oct 24 2016 00:00:00 GMT+0530 (IST),
      'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST),
  },
  {
      'startDate':Mon Oct 31 2016 00:00:00 GMT+0530 (IST)
      'endDate':Wed Nov 02 2016 00:00:00 GMT+0530 (IST),
  }]
  </code></pre>