Mechwd
Mechwd

Reputation: 400

Format time in Angular

I have a time field coming from the database that is formated like "14:37:39". I need it formated to be like "2:37 PM". I have tried to use the date filter (which formats time as well), but with no luck.

Help!

<span class="time">{{ feeding.time | date: "shortTime" }}</span>

Upvotes: 0

Views: 128

Answers (2)

Anthony Chu
Anthony Chu

Reputation: 37520

You can build your own filter based on the format you're expecting...

angular.module('foo', [])
.filter('formatTime', function ($filter) {
    return function (time) {
        var parts = time.split(':');
        var date = new Date(0, 0, 0, parts[0], parts[1], parts[2]);
        return $filter('date')(date, 'h:mm a');
    };
});

{{ '14:37:39' | formatTime }}

Outputs 2:37 PM

Live Demo

Upvotes: 2

Josep
Josep

Reputation: 13071

I think that your problem is that feeding.time is a string and the date filter is expecting a date.

Upvotes: 1

Related Questions