Kevin
Kevin

Reputation: 4848

Why isn't my date formatting as expected?

My date from the database table looks like this:

"2017-12-07 14:42:38.0611177 +00:00"

I'm trying to format it in my HTML like this:

<td>{{ note.CreatedAt | date : "MMM d, y" }}</td>

I was expecting the date to look like this:

Dec 7, 2017

But, the result looks like this: date is not in expected format

Can someone show me what I'm doing wrong?

Upvotes: 2

Views: 46

Answers (1)

Zooly
Zooly

Reputation: 4787

You have to convert your date as a timestamp before filter it.

Aleksey remark is a good point, you can get timestamp from controller:

angular.module('app').controller('dateCtrl', ['$scope', function($scope){
  var date = "2017-12-07 14:42:38.0611177 +00:00";
  $scope.createdAt = new Date(date).valueOf();
}]);

And use it in your view:

<span>{{createdAt |  date : "MMM d, y"}}</span>

Top example will output: Dec 7, 2017

Plunker Demo

For more informations, refer to AngularJS documentation concerning [date filter].2

Upvotes: 2

Related Questions