Ibrahim Maher Ibrahim Maher - 4 years ago 101
AngularJS Question

Format datetime object in Array and display in ng-repeat AngularJS

I've a function that gets a list of Users. Each individual user has a

last_logged
field which shows the last logged in time in this format
2017-02-04 19:54:47
. What I need to do is display that field on the page in this format
Saturday, February 2, 2017 - 19:54:47
.

Currently the time format is
2017-02-04 19:54:47
(native datetime format)
How can I format it such that it shows
Saturday, February 2, 2017 - 19:54:47


<table>
<thead>
<tr>
<th>Username</th>
<th>Last Login</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{ user.username }}</td>
<td>{{ user.last_login }}</td>
</tr>
</tbody>
</table>

NNR NNR
Answer Source

Try this

{{calculateTime(last_login) | date: 'EEEE, MMMM M, yyyy - HH:mm:ss'}}

Demo

app.controller('MainController', function($scope) {
  $scope.last_login = "2017-02-04 19:54:47";
  $scope.calculateTime = function(dt) {
    return new Date(dt).getTime();
  }
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download