2015-12-31 20 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時區