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?
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 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
For angular over V2 use shortTime
with date
pipe:
{{ myDate | date : 'shortTime' }}
'shortTime': equivalent to 'h:mm a' (9:03 AM).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With