Sequelize createdAt and updatedAt timestamps are wrong
Asked Answered
Z

3

6

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

Zosima answered 27/4, 2018 at 7:49 Comment(0)
O
7

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

Olinger answered 2/2, 2019 at 7:30 Comment(1)
I am using the v6 version of sequelize and the docs state this: timezone: The timezone configured on the MySQL server. This is used to type cast server date/time values to JavaScript Date object and vice versa. This can be 'local', 'Z', or an offset in the form +HH:MM or -HH:MM. (Default: 'local') So your answer points to the correct attribute to set but the value will cause an error, at least in v6 and onwards, using +00:00 has done the trick for me npmjs.com/package/mysql#connection-optionsFaerie
K
5

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'
});
Kraus answered 27/4, 2018 at 9:52 Comment(4)
then I get this error throw new Error('Setting a custom timezone is not supported by SQLite, dates are always returned as UTC. Please remove the custom timezone parameter.');Zosima
and what if you try this: dialectOptions: { useUTC: false, },Kraus
no luck, currently dialectOptions are only supported for 'mysql', 'postgres', 'mssql'Zosima
Is anyone using 'mariadb' as dialect with a European timezone please?Whitleather
D
1

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": {
...

} }

Dragonet answered 28/10, 2018 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.