我想在nodejs中制作restful应用。
服务器:centos 7 64x
数据库:postgresql
附加:express,sequelize
表:带有时区的日期时间
当使用sequelize从数据库选择行时,created_at列会给出错误的时间。时间增加了5个小时。
我将centos的时区配置更改为+5(塔什干/亚洲),同时更改了postgresql的时区配置为+5,在数据库中Datetime的显示是正确的。
但是当我选择它时,它转换成这样:
"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"
}
}
如何在正确的时区格式中从数据库中选择行?