24

I have an ng-repeat element which will loop through $http.get() result.

<tr ng-repeat="blog in posts">
     <td style="text-align:center">{{ $index+1 }}</td>
     <td>{{ blog.title }}</td>
     <td>
         {{ blog.author.name }}
     </td>
     <td>
         {{ blog.created_at | date:'MMM-dd-yyyy' }}
     </td>
</tr>

I have created_at as timestamp in MySQL database table. And I am using angular.js v1.0.7.

I am getting the same output from db table and date filter is not working. How can I solve this?

My ajax call,

$http({method: 'GET', url: 'http://localhost/app/blogs'}).
success(function(data, status, headers, config) {
    $scope.posts = data.posts;
}).
error(function(data, status, headers, config) {
    $scope.posts = [];
});

5 Answers 5

42

The date passed to the filter needs to be of type javascript Date.

Have you checked what the value blog.created_at is displayed as without the filter?

You said your backed service is returning a string representing the date. You can resolve this in two ways:

  1. Make your server-side code return a json date object
    • check how the server side code serialize the json that it returns
  2. Write your own filter which accepts the string date and returns date in the required format
    • Note: you can call the angular filter in your own filter

You can write your own filter as follows:

app.filter('myDateFormat', function myDateFormat($filter){
  return function(text){
    var  tempdate= new Date(text.replace(/-/g,"/"));
    return $filter('date')(tempdate, "MMM-dd-yyyy");
  }
});

And use it like this in your template:

<td>
  {{ blog.created_at | myDateFormat }}
</td>

Rather than looping through the returned array and then applying the filter

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

4 Comments

I have noticed this in some of answers. But how can I achieve this in my $http call ? Both time I am getting 2013-10-08 00:00:00
Thanks, I have described that I have my date in timestamp format,and I have expected clear code with my snippet. So to convert this I have to use some regex. I have posted my final working result.
adding split('.')[0] works for me .. so the new tempdate looks likes as -- var tempdate= new Date((text.replace(/-/g,"/").split('.')[0]));
there is built in methods for this. Why do you use a framework
15

From the server side, it returns the created_at as string from the laravel eloquent.

This can be solved using this javascript,

new Date("date string here".replace(/-/g,"/"));

So the code,

$http({method: 'GET', url: 'http://localhost/app/blogs'}).
success(function(data, status, headers, config) {
   angular.forEach(data.posts, function(value, key){
     data.posts[key].created_at = new Date(data.posts[key].created_at.replace(/-/g,"/"));
   }
   $scope.posts = data.posts;
}).
error(function(data, status, headers, config) {
    $scope.posts = [];
});

2 Comments

why not angular forEach
@Vignesh Yes, I should utilize that.
9

you can add a custom Filter which convert string to date, as the following code:

app.filter('stringToDate',function ($filter){
    return function (ele,dateFormat){
        return $filter('date')(new Date(ele),dateFormat);
    }
})

then use this filter in any template as the following code:

<div ng-repeat = "a in data">{{a.created_at |stringToDate:"medium"}}</div>

Comments

6

You can create new Date(/*...*/) based on fetched data from $http.get, like:

$scope.date = new Date('2013', '10', '28'); // for example

Anyways you can see this Demo in Plunker.

Hope it will help you

1 Comment

Thanks, I have described that I have my date in timestamp format. So to convert this I have to use some regex. I have posted my final working result.
2

When record date variable remember to use 'new' keyword as below:

var time = new Date();

otherwise if you write as this:

var time = Date();

Date is called as a function and a date time string will be returned which cannot be used as input to the filter.

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.