Rizka Nugroho Rizka Nugroho -4 years ago 119
AngularJS Question

How to subtract date in angularjs

I try make list from curent date until last 31 days. I try use

ng-repeat
to make list today until 31 day. I can make list but not date. I try code like this

HTML

<ion-list>
<ion-item class="animate-repeat" ng-repeat="i in rep">
{{trip}}
{{besok}}
</ion-item>
</ion-list>


JS

$scope.rep = [];
for (var i=0; i < 31; i++) {
$scope.trip = new Date();
$scope.besok = $scope.trip.setDate($scope.trip.getDate()- i);
$scope.rep.push(i);
}


enter image description here

and it will show like this. It cannot subtract day. How to reduce it properly in angularjs ? please help me solved this problem. Thanks

Answer Source

Try like this. i know this is not angularjs code. same logic would work in both

var rep = [];
for (var i=0; i < 31; i++) {  
   var trip = new Date();
   
   var besok =new Date(trip.getTime() - i*(24*60*60*1000));
   rep.push(besok);
}
console.log(rep);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download