Codehan25
Codehan25

Reputation: 3014

How to format a date in AngularJS (Datefilter)

I need to format a date with the AngularJS date-filter. Unformatted the date looks like this:

2017-03-02T00:00:00.000Z

What I want to achieve:

03/02/2017

I use the date-filter like this:

<p data-ng-bind="selectedEvent.start" | date:'MM/dd/yyyy'>{{selectedEvent.start}}</p>

And this is what I get:

Thu Mar 02 2017 00:00:00 GMT+0000

What am I doing wrong?

Upvotes: 1

Views: 923

Answers (2)

rrd
rrd

Reputation: 5957

You're right and wrong.

<p data-ng-bind="selectedEvent.start | date: 'MM/dd/yyyy'"></p>

You were binding the date, but then had the filtering outside the quote. Then you had the double braces inside the

too, which was a duplicate.

Upvotes: 2

Bjarne Gerhardt-Pedersen
Bjarne Gerhardt-Pedersen

Reputation: 1144

You are closing your model value before adding the filter. Instead of writing

<p data-ng-bind="selectedEvent.start" | date:'MM/dd/yyyy'>{{selectedEvent.start}}</p>

Write this

<p data-ng-bind="selectedEvent.start | date:'MM/dd/yyyy'"></p>

And you dont need {{}} afterwards, since you are binding the value already.

Upvotes: 1

Related Questions