Luis Alvarez
Luis Alvarez

Reputation: 99

Moment JS - Adds two days

Here is my current MomentJS code:

var date = moment($scope.dt);

for (var i = 0; i < parseInt($scope.numPagos); i++) {
    $scope.resultados.push({
        'numerodecuota' : i + 1,
        'fechas' : date.add(1, 'days').format("MM/DD/YYYY"),
        'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
        'interes' : Math.round(interes),
        'capital' : $scope.importeprestamo / $scope.numPagos,
        'fechaunix' : date.add(1, 'days').unix()
    }); 

}// End for loop

And this is the result:

Result

It has to be:

And so on.

Upvotes: 0

Views: 118

Answers (3)

Do you have two times method "date.add(1, 'days')";

try this:

       var date = moment($scope.dt);

        for (var i = 0; i < parseInt($scope.numPagos); i++) {
            var current = date.add(1, 'days');


            $scope.resultados.push({
                'numerodecuota' : i + 1,
                'fechas' : current.format("MM/DD/YYYY"),
                'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
                'interes' : Math.round(interes),
                'capital' : $scope.importeprestamo / $scope.numPagos,
                'fechaunix' : current.unix()
            }); 

        }// End for loop

Upvotes: 1

Naghaveer R
Naghaveer R

Reputation: 2944

Because you are adding one day twice in your code

Once here

'fechas': date.add(1, 'days').format("MM/DD/YYYY"),

Again Here

  'fechaunix': date.add(1, 'days').unix()

Add only once. see the example

var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
  var date = moment();
  $scope.numPagos="5";
  $scope.resultados=[];

  for (var i = 0; i < parseInt($scope.numPagos); i++) {
    $scope.resultados.push({
      'numerodecuota': i + 1,
      'fechas': date.add(1, 'days').format("MM/DD/YYYY"),
      //'pagos': Math.round($scope.importeprestamo / $scope.numPagos + interes),
      //'interes': Math.round(interes),
      //'capital': $scope.importeprestamo / $scope.numPagos,
      'fechaunix': date.unix()
    });

  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  {{resultados}}
</div>

Upvotes: 1

Sean Kendle
Sean Kendle

Reputation: 3609

Note: It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.

http://momentjs.com/docs/

Looks like you're adding 1 day, and then adding another day for the fechaunix date. Try just setting fechaunix to date once it's already been added:

$scope.resultados.push({
     'numerodecuota' : i + 1,
     'fechas' : date.add(1, 'days').format("MM/DD/YYYY"),
     'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
     'interes' : Math.round(interes),
     'capital' : $scope.importeprestamo / $scope.numPagos,
     'fechaunix' : date.unix()
}); 

Upvotes: 3

Related Questions