sradha
sradha

Reputation: 2244

Date format filter is not working angular js

I am new to angular js . I am trying to give some date format like Nov-24-2016, but its not working at all . Below is my code.

{{ x.created | date:" MM-d-y" }}

Here I want this format Nov-24-2016

and x.created value = 2016-11-24 08:02:21

Any suggestion? Thank you

Upvotes: 1

Views: 2317

Answers (5)

MOgorodnik
MOgorodnik

Reputation: 1

I had similar problem. It's fix after 2 steps, change document encoding to utf-8 without BOM and rewrite manually data of time. Like this - date: 2013-12-02T17:57:28.556094Z

Upvotes: 0

philipooo
philipooo

Reputation: 1049

{{ x.created | date: "MMM-d-y" }} renders to Nov-24-2016

Btw: 2016-11-24 08:02:21 seems not to be a valid ISO string, https://www.w3.org/TR/NOTE-datetime. You could check this.

Upvotes: 0

Lacrioque
Lacrioque

Reputation: 358

Have a look at the documentation: https://docs.angularjs.org/api/ng/filter/date

x.created must be either a Date object or a timestring with milliseconds since UTC epoch.

So either create a manual filter for it or parse your datestring to a Date object.

Upvotes: 2

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

you can create a custom

.filter('datetime', function($filter){
    return function(input){
      if(input == null){ return ""; } 

      var _date = $filter('date')(new Date(input),'MM-dd-yyyy');         
      return _date.toUpperCase();

    };
});


{{ x.created | datetime }}

Upvotes: 3

ranakrunal9
ranakrunal9

Reputation: 13558

To use date Pipe x.created must be date object or a number (milliseconds since UTC epoch) or an ISO string as per DatePipe

Upvotes: 1

Related Questions