Home Why isn't my date formatting as expected?
Reply: 1

Why isn't my date formatting as expected?

Kevin Published in 2017-12-07 16:12:25Z

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:

Can someone show me what I'm doing wrong?

Zooly Reply to 2017-12-07 16:33:40Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333014 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO