Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize createdAt and updatedAt timestamps are wrong

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

like image 912
Roledenez Avatar asked Apr 27 '18 07:04

Roledenez


3 Answers

Create database like this you'll get automatically created at updated at according local time

const sequelize = new Sequelize("db_name", "username", "password", { logging: false, host: "localhost", dialect: "mysql", dialectOptions: { // useUTC: false, //for reading from database dateStrings: true, typeCast: true, timezone: "+05:30" }, timezone: "+05:30", //for writing to database operatorsAliases: false });

like image 191
user11004463 Avatar answered Oct 18 '22 16:10

user11004463


I'm late but this works for me: new Sequelize(db,user,pass, {timezone: "-05:00"}) where "-05:00" is offset time.

En config.json too...

{
"development": {
"database": "db",
"username": "user",
"password": "pwd",
"logging": false, // verbose
"host": "localhost",
"dialect": "mysql",
"operatorsAliases": false, 
"timezone": "-05:00"
},
"test": {
...
},  
"production": {
...

} }

like image 29
marqueTTronica Avatar answered Oct 18 '22 16:10

marqueTTronica


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'
});
like image 20
Adam Avatar answered Oct 18 '22 14:10

Adam