我想在Node.js中创建宁静的应用程序
服务器:centos 7 64x 数据库:postgresql 附加:表达,续集 表格:带时区的日期时间
当我从数据库中选择带有序列化的行时,created_at列给我错误的时间。日期时间增加了5小时。
我将centos的时区配置更改为+5(塔什干/亚洲) 还要将postgresql时区配置更改为+5 显示时,数据库中的日期时间正确。
但是当我选择它时,它会转换成这样
“ createdAt”:“ 2018-08-12T17:57:20.508Z”
在数据库列中显示
2018-08-12 22:57:20.508 + 05
config.json
"development": {
"username": "postgres",
"password": "postgres",
"database": "zablet",
"host": "127.0.0.1",
"dialect": "postgres",
"timezone": "Tashkent/Ashgabat",
"define": {
"charset": "utf8",
"dialectOptions": {
"collate": "utf8_general_ci"
},
"freezeTableName": true
}
}
index.js
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require('../config/config.json')[env];
var db = {};
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
更新了config.json
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "postgres",
"host": "127.0.0.1",
"dialect": "postgres",
"define": {
"charset": "utf8",
"dialectOptions": {
"collate": "utf8_general_ci"
},
"freezeTableName": true
},
"dialectOptions": {
"useUTC": false
},
"timezone": "+05:00"
}
}
如何从数据库中以正确的时区格式选择行?
答案 0 :(得分:0)
这是因为您设置的时区仅用于写入数据库,而不用于读取。这可以正常工作。我正在使用这个。
development: {
username: 'postgres',
password: 'postgres',
database: 'YOUR_DATABASE_NAME',
host: '127.0.0.1',
port: 5432,
dialect: 'postgres',
dialectOptions: {
useUTC: false, // for reading from database
},
timezone: '+05:30', // for writing to database
},