0

hi I am making amortization by looping and losing a few months when the input is in the date (29,30,31)

Here is some piece of my 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() > 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 <= jumbulan; i++) {
                        var nextMonth = angular.copy(loopdate);
                        nextMonth.setMonth(nextMonth.getMonth() + 1);

                        if (nextMonth < 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 < 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 < data.data.length; i++) {
                            data.data[i].amount = vm.bebanPertamax;
                            vm.amortisasirequest.jurnalDetail.push(data.data[i]);
                        }
                        vm.refreshData();
                    });
                }
            })

most of the data that contains empty months are in February, looping over skips in February:

data

what should i change about it?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • can you simplify the question a bit? – Saikat Chakrabortty Jul 28 '20 at 13:35
  • You can use the code I've written: ```function addMonth(dt, dom) { var nextMonth=new Date(dt); nextMonth.setMonth(nextMonth.getMonth()+1); nextMonth.setDate(dom); while( (12+nextMonth.getMonth())%12 > (12+dt.getMonth()+1)%12) { nextMonth.setDate(nextMonth.getDate()-1); } return nextMonth; }``` `dt` is the starting month, `dom` is the starting day-of-month. After `addMonth` returns, check if still in range (and if over, but maturity not hit - add maturity). It's on you to pass valid `dt` (Date object) and `dom` (positive integer 1-31). – iAmOren Jul 28 '20 at 14:40

1 Answers1

1

Since you started from Nov 30th and went on, month by month, till Jan 30th the "next month" is calculated as "Feb 30th" which overlaps into March...

Example code:

const date = new Date('Jan 30, 2020 00:00:01');
console.log(date.getMonth()); // (January gives 0)
date.setMonth(date.getMonth() + 1); 
console.log(date.getMonth());// (gives 2 == March)
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129