服务端使用mongoose操做mongodb,其中Schema中的日期字段定义以下:mongodb
date: {type:Date, default:Date.now},//操做日期
插入到mongodb中adte为:"date" : ISODate("2015-08-15T03:26:36.086Z"),api
与当前时间相差8小时,客户端采用angular进行操做,在页面上展现的内容为:2015-08-15T03:26:36.086Z网络
如今经过使用moment.js在客户端进行处理,处理方式是定义一个过滤器filter来进行处理.app
(1)安装moment.jssocket
y@y:app01$ bower install --save momentmongoose
(2)定义filterspa
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)使用filtercode
td(style="vertical-align:middle") {{a.date | getLocalTimeFilter}}
此时操做界面时间显示正常:2015-08-15 11:26:36orm