timezone - Sequelize createdAt 和 updatedAt 时间戳错误

标签 timezone timestamp sequelize.js momentjs sequelize-cli

我使用 sequelize 创建了一个带有时间戳的表。当我更新表时,它会自动更新时间戳(即 createdAt 和 updatedAt)。但这些时间与我的本地时间不同。如果我使用 moment 像这样转换时区,我附上了 2 个带有模型脚本的屏幕截图 updateddAt: moment().utc(new Date())它工作正常。有没有办法用当前时区自动更新时间戳?

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

更新表格时
enter image description here

我在电脑上的本地时间
enter image description here

最佳答案

您应该设置 timezone Sequelize 选项中的属性:

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

关于timezone - Sequelize createdAt 和 updatedAt 时间戳错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50057759/

相关文章:

php - PHP 时区的最佳方法

python - 无法在python中使用posix风格的时区解析解析时间字符串

c# - 如何从 CultureInfo 中的属性获取时区

node.js - Sequelize - 如何设计一对一关联?

java - 有什么方法可以让 Quartz 的 DailyTimeIntervalTrigger 在备用时区运行?

hadoop - Hive中的Javascript日期转换

php - 为什么 strtotime($lastmoment) 比 time() 大?

postgresql - 如何在postgresql中将bigint(以毫秒为单位的时间戳)值写为时间戳

javascript - 运行 Node server.js 并重新播种数据库时,表被删除。 Sequelize

mysql - Azure MySql + Sequelize 之间的连接