2014-07-09 12 views
14

Jak sformatować siatkę kątową, która odbiera elementy, które mają właściwość date time o nazwie startTime i endTime, aby wyświetlać czas w formacie AM/PM? Teraz używam:Wyświetlanie czasu w formacie AM/PM z siatką kątową

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

i oczywiście "tt" pokazuje zamiast AM lub PM. Czy ktoś wcześniej robił AM/PM w Angular ngGrid?

Odpowiedz

24

Jest prosty, patrz poniżej:

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

To wszystko

Więcej szczegółów na temat date format see this

3

Po prostu musiałem parę rzeczy połączyć. Po pierwsze, filtr:

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; 
     } 
    }); 

Następnie wywołanie funkcji na gridOptions:

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

A ty cały zestaw.

2

Właśnie napotkałem ten sam problem i znalazłem prostsze rozwiązanie przy użyciu filtru daty kątowej. Trzeba tylko zmienić tt do a, podobnie jak

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

zobaczyć kątową na refference dla filtra datę - https://docs.angularjs.org/api/ng/filter/date

Powiązane problemy