A more elegant date formatting solution is to create a custom filter because it is cleaner and you can reuse it everywhere:
Filter:
myApp.filter('myMillisecondsToUTCDate', [function() {
return function(milliseconds) {
var date = new Date(milliseconds);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
};
}]);
Controller:
myApp.controller('MyCtrl', function($scope, $filter) {
$scope.testDate = 1429831430363;
$scope.formattedDate = $filter('myMillisecondsToUTCDate')($scope.testDate);
});
Html:
<div ng-controller="MyCtrl">
Format a date using a custom filter: {{testDate | myMillisecondsToUTCDate}}<br/>
Formatted date {{formattedDate}}
</div>
Check out the demo script
source
share