I need to parse some dates from strings coming from JSON data. How to do that in Angular?
I tried to apply Date.parse
directly (Date.parse({{ item.in_time}})
) but that didn't work. I also tried the json filter {{ item.out_time | json | date:'shortTime'}}
. I guess I need a custom filter (like this: How to format a JSON date in AngularJS but I don't know where to add it in my app
var mockData = [{
"field1": "Annie Parker",
"field2": "Structural Analysis Engineer",
"field3": "Mybuzz",
"in_time": '1483251003000', // this shows out correctly because it's pre-parsed
"out_time": '1483251003000',
}, {
"field1": "John Diaz",
"field2": "Tax Accountant",
"field3": "Tagchat",
"in_time": '01 Jan 2017 8:00:03',
"out_time": '01 Jan 2017 9:00:03',
}, {
"field1": "Sean Bailey",
"field2": "Senior Developer",
"field3": "Voonder",
"in_time": '01 Jan 2017 8:00:03',
"out_time": '01 Jan 2017 10:00:03',
}, {
"field1": "Wanda Webb",
"field2": "Biostatistician I",
"field3": "Realcube",
"in_time": '01 Jan 2017 5:00:03',
"out_time": '01 Jan 2017 5:00:03',
}];
var App = angular.module('myApp', []);
function DataCtrl($scope, $http) {
$scope.items = mockData;
}
table,
th,
td {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp'>
<div ng-controller='DataCtrl'>
<h1>Data</h1>
<table class="row-border hover table table-bordered cb-data-table">
<tr>
<th>In Time</th>
<th>Out Time</th>
<th>Diff</th>
</tr>
<tr ng-show="!items.length">
<td colspan="3" style="text-align: center">~ No Any Records ~</td>
</tr>
<tr ng-repeat="item in items">
<td>{{ item.in_time | date:'shortTime'}}</td>
<td>{{ item.out_time | json | date:'shortTime'}}</td>
<td></td>
</tr>
</table>