Angular Testbed
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"></script>
<body ng-app="app">
<div ng-model='testDate' unix-timestamp >
{{testDate}}
</div>
</body>
JavaScript
angular.module('app', [])
.run(function ($rootScope, $timeout) {
$rootScope.testDate = 12867053213;
$timeout(function () {
$rootScope.testDate = 1286705410;
}, 1000);
})
.directive('unixTimestamp', function () {
return {
require: 'ngModel',
priority: 1,
link: function (scope, element, attr, ngModelCtrl) {
ngModelCtrl.$formatters.push(function (timestamp) {
if (timestamp) {
var date = moment.unix(timestamp).toDate();
return date;
} else {
return null;
}
});
ngModelCtrl.$parsers.push(function (date) {
if (date) {
var timestamp = moment(date).unix();
return timestamp;
} else {
return null;
}
});
}
};
})
});