1

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

3
  • what's problem in that? Commented Mar 5, 2018 at 7:02
  • Same format only displaying Commented Mar 5, 2018 at 7:08
  • check my answer that might help. Commented Mar 5, 2018 at 7:10

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

6 Comments

Are you getting the response? $scope.data = data.data; after this step, you are trying right not outside?
How to get in POST API
use $http.post for calling post apis.
Here i want to apply $scope.data = response.data.data; $scope.name = $scope.data.name; $scope.created_at = $scope.data.created_at; change date format
you cant do $scope.data.name because $scope.data is not an object is an array. You don't need to store the formatted date in DB because It may create problems while performing sort or search operations.
|
0

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'}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.