【问题标题】:Sequelize DATE record different from model objectSequelize DATE 记录与模型对象不同
【发布时间】:2015-12-31 01:07:44
【问题描述】:

我遇到了一个问题,当我将记录用作视图中的对象时,数据库中记录的日期与表示的日期不匹配。我似乎无法弄清楚为什么记录会以不同的方式表示,但我注意到在我的数据库中,日期与仅以yyyy-mm-dd 格式记录日期一致,时间戳为00:00:00,例如2015-12-26 00:00:00。这是应该保存的正确格式。出于某种原因,当我显示此值时,它以 Fri Dec 25 2015 19:00:00 GMT-0500 (EST) 格式显示,日期晚一天,因为它正在记录并以 GMT 显示,即 5几个小时之后,为什么两者不匹配是有道理的。我看不到这是在我的模型或路线中设置的位置。有帮助解决这个问题吗?

AnnotationDate 是我引用的列

这是我的模型:

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;
}

以下是值作为对象传递的方式:

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);
        })
    });

【问题讨论】:

    标签: mysql date datetime express sequelize.js


    【解决方案1】:
    new Sequelize(db, user, pass, {
      timezone: '+01:30'
    });
    

    当您定义与 mysql 数据库的新 Sequalize 连接时,如果您未定义时区,则应提及您的时区,而不是使用 UTC 时区

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-03
      • 2019-03-06
      • 2021-10-10
      • 2019-08-30
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多