Srinivasan
Srinivasan

Reputation: 303

SQL date field not showing properly in Angular.js

I tried showing MSSQL data in a HTML table using Angular JS ng-repeat. My date column field is shown in detailed with timezone etc:

{"date":"2016-12-11 00:00:00.000000","timezone_type":3,"timezone":"UTC"}

I just want to show the date alone. How to format that?

<tr ng-repeat="bill in allbills | filter : search" >
    <td>{{ $index + 1 }}</td>   
    <td>{{ bill.bill_Date }}</td> 
</tr>

Upvotes: 2

Views: 704

Answers (1)

Divyanshu Maithani
Divyanshu Maithani

Reputation: 14986

You need to use the date filter:

{{ date_expression | date : format : timezone}}

Your <td> should look something like this to display only date:

<td>{{ bill.bill_Date | date:'yyyy-MM-dd'}}</td>

EDIT

You can also write a custom filter since you are already getting the date in correct format. Check this fiddle with working code:

HTML

<div ng-app="myApp" ng-controller="myCtrl">
  <span>{{ bill.bill_Date.date | date}}</span>
</div>

JS

angular.module('myApp', [])
  .controller('myCtrl', function($scope) {
    $scope.bill = { // from row set
      'bill_Date': {
        "date": "2016-12-11 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "UTC"
      }
    };
  })
  .filter('date', function() {
    return function(input) {
      return input.split(' ')[0];
    }
  });

Upvotes: 1

Related Questions