title: Javascript date loop missing out some months
body: <p>hi I am making amortization by looping and losing a few months when the input is in the date (29,30,31)</p>
  <p>Here is some piece of my code :</p>
  <pre><code>if (vm.amortisasirequest.pR_StartDate != vm.amortisasirequest.pR_MaturityDate) {
                      var startdate = new Date(vm.amortisasirequest.pR_StartDate);
                      var endate = new Date(vm.amortisasirequest.pR_MaturityDate);
                      vm.kartuAmortisasi = [];
                      if (endate.getFullYear() == startdate.getFullYear()) {
                          jumbulan = endate.getMonth() - startdate.getMonth();
                      } else if (endate.getFullYear() &gt; startdate.getFullYear()) {
                          jumbulan = (endate.getMonth() + ((endate.getFullYear() - startdate.getFullYear()) * 12)) - startdate.getMonth();
                      }
                      vm.totalBeban = vm.amortisasirequest.pO_TotalPurchase / 0.8; //+ (20 / 100 * vm.amortisasirequest.pO_NominalBarang)
                      vm.bebanperbulan = parseFloat(vm.totalBeban / jumbulan).toFixed(2);
                      vm.beban = vm.bebanperbulan;
                      vm.bebanPertamax = vm.beban;
                      var j = 0;
                      var dateNow = new Date();
  
                      for (var i = 0; i &lt;= jumbulan; i++) {
                          var nextMonth = angular.copy(loopdate);
                          nextMonth.setMonth(nextMonth.getMonth() + 1);
  
                          if (nextMonth &lt; dateNow) { 
                              vm.beban = parseFloat(vm.beban) + parseFloat(vm.bebanperbulan);
                              vm.beban = parseFloat(vm.beban).toFixed(2);
                              vm.bebanPertamax = vm.beban;
                          }
  
                          else {
                              // Periode Terakhir
                              if (i == jumbulan) {
                                  vm.totalAmor = 0;
                                  for (var k = 0; k &lt; vm.kartuAmortisasi.length; k++) {
                                      vm.totalAmor = vm.totalAmor + parseFloat(vm.kartuAmortisasi[k].amortizeAmount);
                                  }
                                  vm.beban = vm.totalBeban - vm.totalAmor;
                              }
                              vm.kartuAmortisasi.push({ periode: j + 1, valueDate: angular.copy(loopdate), amortizeAmount: parseFloat(vm.beban).toFixed(2) });
                              if (j == 0)
                                  vm.kartuAmortisasi[0].isProceed = true;
                              vm.beban = vm.bebanperbulan;
                              //vm.beban = parseFloat(vm.beban).toFixed(2);
                              j++;
                          }
                          loopdate.setMonth(loopdate.getMonth() + 1);
                      }
  
                      vm.amortisasirequest.listKartuAmortisasi = vm.kartuAmortisasi;
  
                      vm.amortisasirequest.pO_NilaiBuku = parseFloat(vm.totalBeban) - parseFloat(vm.bebanPertamax);
  
                      vm.amortisasirequest.jurnalDetail = [];
  
                      AmortisasiRequestService.processJurnalOtomatis({ poNo: vm.selectedPurchaseOrder.poId, narrative: vm.amortisasirequest.requestId }).then(function (data) {
                          for (var i = 0; i &lt; data.data.length; i++) {
                              data.data[i].amount = vm.bebanPertamax;
                              vm.amortisasirequest.jurnalDetail.push(data.data[i]);
                          }
                          vm.refreshData();
                      });
                  }
              })
  </code></pre>
  <p>most of the data that contains empty months are in February, looping over skips in February:</p>
  <p><a href="https://i.stack.imgur.com/zllaM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zllaM.png" alt="data" /></a></p>
  <p>what should i change about it?</p>