phantom phantom - 1 year ago 48
Javascript Question

angular format date from api response

I am receiving a date from an API that returns data, with a date, as follows:

created_at:"Mon May 30 12:28:00 +0000 2016"

By using Angular I would like to format it so it looks like this:

30 May 12:28

I have tried the following but unfortunately it doesn't work

<p>{{post.created_at | date:'dd MMM HH:mm'}}</p>


You need to convert the date string into a Date() object before submitting into the date filter.

// in controller
post.created_at = new Date(post.created_at);

// template can remain same
<p>{{post.created_at | date:'dd MMM HH:mm'}}</p>

// test code 

console.log($filter('date')(new Date('Mon May 30 12:28:00 +0000 2016'), 'dd MMM HH:mm'));

//30 May 05:28 for me, because I'm GMT-0700.