ngplayground
ngplayground

Reputation: 21617

AngularJS format date doesn't format

{{ map.thedate }}

The above outputs 2014-06-29 16:43:48

When I try the below code it still shows the same date as above.

{{ map.thedate | date:'medium' }}

Upvotes: 0

Views: 67

Answers (2)

harishr
harishr

Reputation: 18055

your date is not in ISO format. Use a filter to convert your input to a date and then apply the date filter

app.filter("toDate", function () {
    return function (input) {
        return new Date(input);
    }
});

Then in html markup:

{{map.thedate | toDate | date:'medium'}}

Upvotes: 1

user4097807
user4097807

Reputation:

Conver your date, here is an example:

for (var i=0; i<map.length; i++) {
    var unixTime = (new Date(map[i].thedate)).getTime();         
    map[i].thedate= unixTime;        
}        

AngularJS will not accept that format of date, this is an easy solution to iterate over your dataset and convert it to a format it will accept.

Another alternative is to do the same thing about on the server-side. Just iterate over it and convert it to a unix timestamp.

This is a description from the docs. on what it will accept:

Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.

Upvotes: 1

Related Questions