Sequelize createdAt and updatedAt timestamps are wrong

Roledenez picture Roledenez · Apr 27, 2018 · Viewed 11.7k times · Source

I created a table with timestamps using sequelize. when I am updating the table, it automatically updates the timestamp (i.e createdAt and updatedAt). but these times are different from my local time. I have attached herewith 2 screenshots with the model script if I use moment to convert timezone like this useupdateddAt: moment().utc(new Date()) it works fine. is there a way to automatically update the timestamps with current timezone?

    'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('States', {

      hashCode: {
        type: Sequelize.STRING,
          unique:true,
          autoIncrement:false
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('States');
  }
};

when updateding the table enter image description here

my local time in the pc enter image description here

Answer

Adam picture Adam · Apr 27, 2018

You should set the timezone property in sequelize options:

const sequelize = new Sequelize({
  database: 'db_name',
  username: 'username',
  password: null,
  dialect: 'mysql'
  timezone: 'utc', // your timezone comes here, ex.: 'US/Hawaii'
});