angularjssmart-table

st-table convert string to date


I have json object array binded to a table. Objects have a date column which is in date format but string. I need to change date format.

I tried;

<tbody>
<tr ng-repeat="row in table_data">
  <td>{{row.availabledate | date:'MMMM dd, yyyy'}}</td>
</tr>
</tbody> 

But, since it is string it is not formatted. I dont want to convert it in a loop for the sake of performance. Is there a way to convert this string to date and then format it on html part?


Solution

  • One way is to make your own filter, then use the date filter.

    This crude/raw filter expects a YYYYMMDD string and creates a date object from it that you can then use angular's date filter on.

    app.filter('strToDate', function() {
    
      return function(input) {
        var year        = input.substring(0,4);
        var month       = input.substring(4,6);
        var day         = input.substring(6,8);
        // Do filter work here
    
        return new Date(year,month,day);
    
      }
    
    });
    

    and then

    <td>{{row.availabledate | strToDate | date:'MMMM dd, yyyy'}}</td>
    

    Here's a Plunker link with working example: https://plnkr.co/edit/hn5StpktiMLq2gWHAszu?p=preview

    You could also just make your filter return the formatted date.