0

I am unable to change date format in my response object

How to change this ?

$http({
            method: 'GET',
            url: 'api/url'
        }).success( function (data) {
                $scope.reports = data.data;
                console.log($scope.reports);
                    $scope.reports = $scope.reports.map(obj =>{
                        obj.created_at  = new Date(obj.created_at);
                        return obj;
                    });
          }).error(function (response){
                console.log("error");  
       });

I try to change date format with above code

I got this response

{  
   "my2":[  
      {  
         "id":5,
         "cuid":20,
         "name":"my2",
         "month":"04",
         "created_at":"2018-04-01 00:00:00",
         "updated_at":"2018-04-11 00:00:00",
         "time":"04:32 PM",
         "status":"D"
      },
      {  
         "id":4,
         "cuid":20,
         "name":"my2",
         "month":"04",
         "created_at":"2018-04-02 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"12:10 PM",
         "status":"P"
      },
   ],
   "my":[  
      {  
         "id":44,
         "cuid":21,
         "name":"my",
         "month":"04",
         "created_at":"2018-04-12 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"09:08 PM",
         "status":"P"
      }
   ],
   "Testing":[  
      {  
         "id":43,
         "cuid":19,
         "name":"Testing",
         "month":"04",
         "created_at":"2018-04-12 00:00:00",
         "updated_at":"2018-04-12 00:00:00",
         "time":"09:05 PM",
         "status":"P"
      }
   ]
}

in html i try this

{{data.created_at | date: 'dd'}}

But I got error

TypeError: $scope.reports.map is not a function

I think because of an it's object so that this problem

How to fix this ?

br.julien
  • 3,158
  • 2
  • 22
  • 42
My testing
  • 45
  • 2
  • 8

1 Answers1

0

I suggest you to do a forEach using angularjs (It handles the two-way binding)

angular.forEach($scope.reports, function(report){
    report.createdAt = new Date(report.createdAt);
}
Sajjad Shahi
  • 332
  • 1
  • 11