服務端使用mongoose操作mongodb,其中Schema中的日期字段定義如下:
date: {type:Date, default:Date.now},//操作日期
插入到mongodb中adte為:"date" : ISODate("2015-08-15T03:26:36.086Z"),
與當前時間相差8小時,客戶端采用angular進行操作,在頁面上展示的內容為:2015-08-15T03:26:36.086Z
現在通過使用moment.js在客戶端進行處理,處理方式是定義一個過濾器filter來進行處理.
(1)安裝moment.js
y@y:app01$ bower install --save moment
(2)定義filter
angular.module('angularFullstackTestApp')
.controller('AdviceCtrl', function ($scope,$http,socket) {
$scope.adviceList = [];
/**
* 獲取意見反饋列表
*/
$scope.getAdviceList = function(){
$http.get('/api/advices').success(function(result) {
$scope.adviceList = result;
}).error(function(){
alert("網絡錯誤");
});
};
$scope.getAdviceList();
$scope.$on('$destroy', function () {
socket.unsyncUpdates('thing');
});
}).filter('getLocalTimeFilter',function(){//使用moment.js將ISODate轉換為本地時間
return function(input){
if(input && input.length>11){
return moment(input).format('YYYY-MM-DD HH:mm:ss');
}
return input;
};
});
(3)使用filter
td(style="vertical-align:middle") {{a.date | getLocalTimeFilter}}
此時操作界面時間顯示正常:2015-08-15 11:26:36
