Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying time in AM/PM format with Angular grid

Tags:

angularjs

How would I format an Angular grid that is receiving entities that have a date time property called startTime and endTime to show time in an AM/PM format? Right now I am using:

{ field: 'StartTime', displayName: 'Start Time', cellFilter: 'date:\'hh:mm tt\''},
{ field: 'EndTime', displayName: 'End Time', cellFilter: 'date:\'hh:mm tt\''},

and obviously the 'tt' is showing instead of AM or PM. Has anyone done AM/PM in an Angular ngGrid before?

like image 246
Josh Avatar asked Jul 09 '14 19:07

Josh


4 Answers

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

like image 145
Ali Adravi Avatar answered Sep 20 '22 18:09

Ali Adravi


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

like image 45
Revi Avatar answered Sep 20 '22 18:09

Revi


For angular over V2 use shortTime with date pipe:

{{ myDate | date : 'shortTime' }}

'shortTime': equivalent to 'h:mm a' (9:03 AM).

like image 24
Lucas Avatar answered Sep 18 '22 18:09

Lucas


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.

like image 24
Josh Avatar answered Sep 20 '22 18:09

Josh