cphill
cphill

Reputation: 5914

Sequelize DATE record different from model object

I am running into an issue where the date being recorded in my database does not match the date being represented when I use the record as an object in my view. I can't seem to figure out why the record would be represented differently, but I noticed that in my database the date is consistent with recording just the date in yyyy-mm-dd format with a timestamp of 00:00:00, e.x. 2015-12-26 00:00:00. This is the correct format that it should be save in. For some reason, when I display this value it appears in a format Fri Dec 25 2015 19:00:00 GMT-0500 (EST) with the date being a day behind because it is taking the record and displaying in GMT, which is 5 hours behind which would make sense why the two aren't matching up. I don't see where this is set within my model or route. Any help with figuring this out?

AnnotationDate is the column I am referencing

Here is my model:

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

Here is how the value is being passed as an object:

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

Upvotes: 1

Views: 675

Answers (1)

nikunj sobhashana
nikunj sobhashana

Reputation: 181

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

when you define new sequalize connection to mysql database you should mention your timezone if you not define timezone than it's take UTC time zone

Upvotes: 3

Related Questions