解决mongodb ISODate相差8小时问题

服务端使用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

 



posted @ 2015-08-15 13:43  yshy  阅读(17324)  评论(0编辑  收藏  举报