2015-12-31 19 views
1

我遇到了一个问题,即在我的视图中将记录用作对象时,记录在我的数据库中的日期与代表的日期不匹配。我似乎无法弄清楚为什么记录会有不同的表现,但我注意到在我的数据库中,日期与yyyy-mm-dd格式中的日期一致,时间戳为00:00:00,e.x. 2015-12-26 00:00:00。这是它应该保存的正确格式。出于某种原因,当我显示该值时,它会以Fri Dec 25 2015 19:00:00 GMT-0500 (EST)的格式显示,并且日期比后面的日期晚,因为它正在记录并显示格林尼治标准时间(GMT),比落后5小时这是有道理的,为什么这两个不匹配。我没有看到它在我的模型或路线中设置的位置。解决这个问题的任何帮助?将不同于模型对象的续集DATE记录

AnnotationDate是我引用

这里列是我的模型:

appRoutes.route('/') 

    .get(function(req, res){ 

     models.Annotation.findAll({ 
      where: { 
       userId: req.user.user_id 
      }, 
      attributes: ['annotationId', 'annotationDate'], 
      order: 'annotationDate DESC' 
     }).then(function(annotation){ 
      res.render('pages/test.hbs',{ 
       annotation: annotation, 
       user: req.user, 
       message: req.flash('Flash is back!') 
      }); 
     }) 
    }) 

    .post(function(req, res){ 

     models.Annotation.create({ 

     annotationDate: req.body.annotationDate, 
     userId: req.user.user_id 
     }).then(function() { 
      res.redirect('/app'); 
     }).catch(function(error){ 
      res.send(error); 
     }) 
    }); 

回答

1
new Sequelize(db, user, pass, { 
    timezone: '+01:30' 
}); 

时:

module.exports = function(sequelize, DataTypes) { 

var Annotation = sequelize.define('annotation', { 
    annotationId: { 
     type: DataTypes.INTEGER, 
     field: 'annotation_id', 
     autoIncrement: true, 
     primaryKey: true 
    }, 
    annotationDate: { 
     type: DataTypes.DATE, 
     field: 'annotation_date' 
    }, 
    userId: { 
     type: DataTypes.STRING, 
     field: 'user_id' 
    } 
}, 

{ 
    freezeTableName: true, 
    getterMethods: { 
     annotationDateSlug: function(){ 
      var date = new Date(this.getDataValue('annotationDate')); 
      var month = date.getMonth(); 
      var day = date.getDate(); 
      var year = date.getFullYear(); 

      return month+'/'+day+'/'+year; 

     }, 
    }, 
    classMethods: { 
     associate: function(db) { 
      Annotation.belongsTo(db.User) 
     } 
    } 
}); 
    return Annotation; 
} 

这里是如何的价值被作为一个对象传递你定义新的sequalize连接到mysql数据库你应该提到你的时区,如果你没有定义时区th这需要UTC时区