Learning
Learning

Reputation: 79

Angularjs Date format in GET Response

I want to display date format like this 02-jan-2018

GET Method

$scope.allReport = function() {
       $http({
            method: 'GET',
            url: 'api/admin/report'
        }).success( function (data) {
                $scope.data = data.data;
               console.log($scope.data);
          }).error(function (response){
                console.log("error");  
       });
    }

Response

0:{
created_at:"2018-03-02 00:00:00"
cuid:21
id:64
name:"my"
status:"D"
time:"07:01 PM"
updated_at:"2018-03-02 00:00:00"
}

1:{
created_at:"2018-03-02 00:00:00"
cuid:22
id:65
name:"my1"
status:"P"
time:"06:59 PM"
updated_at:"2018-03-02 00:00:00"
}

I display like this View

<tr ng-repeat="user in data | filter:{name:searchbyname}">
    <td>{{$index+1}}</td>
    <td>{{user.name}}</td>
    <td>{{user.created_at | date : "dd-MMM-yyyy" }}</td>
    <td>{{user.status}}</td>
</tr>

i want to display like created_at this in 02-jan-2018

Upvotes: 1

Views: 91

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 10081

try this, you are getting the date in string format.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr ng-repeat="user in data | filter:{name:searchbyname}">
    <td>{{$index+1}}</td>
    <td>{{user.name}}</td>
    <td>{{user.created_at | date : "dd-MMM-yyyy" }}</td>
    <td>{{user.status}}</td>
</tr>
</table>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.data= [{
    created_at: "2018-03-02 00:00:00",
    cuid: 21,
    id: 64,
    name: "my",
    status: "D",
    time: "07:01 PM",
    updated_at: "2018-03-02 00:00:00",
    }, {
        created_at: "2018-03-02 00:00:00",
        cuid: 22,
        id: 65,
        name: "my1",
        status: "P",
        time: "06:59 PM",
        updated_at: "2018-03-02 00:00:00"
    }];
    
    $scope.data = $scope.data.map(obj =>{
    	obj.created_at  = new Date(obj.created_at); // converting string to date
    	return obj;
    })
});
</script>

</body>
</html>

Upvotes: 2

Tarek.Eladly
Tarek.Eladly

Reputation: 759

An advice form a friend plz use moment you will have a lot of problems with date pipe in angular and moment is more flexible ;)

Angular2 date pipe does not work in IE 11 and edge 13/14

date pipe issue on IE10 - 'Intl' is undefined

https://www.npmjs.com/package/moment

{{now | momentPipe:'DD-MMM-YYYY'}}

Upvotes: 0

Related Questions