简体   繁体   English

Sequelize Nodejs中的时区问题

[英]Timezone Issue In Sequelize Nodejs

I want Sequelize to use my local time for reading and writing to database.我希望 Sequelize 使用我的本地时间来读取和写入数据库。 in Sequelize config file I added timezone: "+04:30", but it is just for writing in database.在 Sequelize 配置文件中,我添加了时区:“+04:30”,但它仅用于写入数据库。 for writing in database when i add用于在我添加时写入数据库

 dialectOptions: { useUTC: false, // -->Add this line. for reading from database },

i get this error:我收到此错误:

Ignoring invalid configuration option passed to Connection: useUTC.忽略传递给连接的无效配置选项:useUTC。 This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection这目前是一个警告,但在 MySQL2 的未来版本中,如果您将无效的配置选项传递给 Connection,则会引发错误

full config file:完整的配置文件:

 const Sequelize = require("sequelize"); const sequelize = new Sequelize("db", "root", "", { dialect: "mysql", port: process.env.SQL_PORT, host: "localhost", charset: "utf8", collate: "utf8_persian_ci", logging: false, dialectOptions: { useUTC: false, // -->Add this line. for reading from database }, timezone: "+04:30", });

Finally I fixed it by getter in my model:最后我在我的 model 中通过 getter 修复了它:

createdAt: {
  type: Sequelize.DATE,
  defaultValue: moment(new Date()).format("YYYY-MM-DD HH:mm:ss"),
  get: function () {
    var isoDateString = new Date(this.getDataValue("createdAt"));
    return new Date(
      isoDateString.getTime() -
        isoDateString.getTimezoneOffset() * 60 * 1000
    );
  },

},

and change my config file to:并将我的配置文件更改为:

const Sequelize = require("sequelize");

const sequelize = new Sequelize("db", "root", "", {
dialect: "mysql",
port: process.env.SQL_PORT,
host: "localhost",
charset: "utf8",
collate: "utf8_persian_ci",
logging: false,
timezone: "+04:30",
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM