Displaying time in AM/PM format with Angular grid

Just encountered this same issue and found a simpler solution using angular's date filter. You just need to change tt to a, like so

{ field: 'StartTime', displayName: 'Start Time', cellFilter: 'date:\'hh:mm a\''}

see angular's refference for the date filter - https://docs.angularjs.org/api/ng/filter/date


It is simple, see this:

{ field: 'createdOn', displayName: 'Created On', width: '180px', 
 cellTemplate: 
"<div class='ngCellText'>{{row.entity.createdOn | date:'MM/dd/yy h:mm:ss a'}}</div>" },

That's it

For more detail about the date format see this


Just had to cobble some things together. First, a filter:

app.filter('ampmtime',
    function () {
        return function (value) {
            if (!value) { return ''; }
            var hours = new Date(value).getHours();
            var minutes = new Date(value).getMinutes();
            var ampm = hours >= 12 ? 'PM' : 'AM';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0' + minutes : minutes;
            var strTime = hours + ':' + minutes + ' ' + ampm;
            return strTime;
        }
    });

Then, the call to the function on the gridOptions:

{ field: 'StartTime', displayName: 'Start Time', cellFilter: 'ampmtime'},
{field:'EndTime', displayName: 'End Time', cellFilter: 'ampmtime'}

And you're all set.

Tags:

Angularjs