更改续集时区 [英] Change sequelize timezone

查看:56
本文介绍了更改续集时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 nodejs 中制作宁静的应用程序

I want to make restful app in nodejs

服务器:centos 7 64x数据库:postgresql附加:表达,续集表:带时区的日期时间

Server: centos 7 64x Database: postgresql Additional: express, sequelize Table: datetime with timezone

当我从数据库中选择带有 sequelize 的行时, created_at 列给了我错误的时间.日期时间增加了 5 小时.

When I selecting rows with sequelize from database, created_at column gives me wrong time. 5 hour added to datetime.

我将 centos 的时区配置更改为 +5(塔什干/亚洲)还将 postgresql 时区配置更改为 +5显示时数据库中的日期时间是正确的.

I change timezone configuration of centos to +5 (Tashkent/Asia) Also change postgresql timezone configuration to +5 Datetime is correct in database when shows.

但是当我选择它时它会变成这样

But when I select it converts to like this

"createdAt": "2018-08-12T17:57:20.508Z"

"createdAt": "2018-08-12T17:57:20.508Z"

在数据库列显示这个

2018-08-12 22:57:20.508+05

2018-08-12 22:57:20.508+05

config.json

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

updated 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"
}
}

如何以正确的时区格式从数据库中选择行?

How can I select rows from database in correct timezone format?

推荐答案

这是因为您设置的时区仅用于写入数据库,而不用于读取.这工作正常.我正在使用这个.

Its because you are setting timezone only for writing to the database, not for reading. This works correctly. I am using this.

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
  },


如果还是不行,就在 connection.js 的 sequelize 构造函数中添加以下属性.


If still it does not work then add the following attribute into the sequelize constructor in the connection.js.

dialectOptions:{useUTC:false},timezone:"+05:30"

类似于下面的代码:

const sequelize = new Sequelize("DB",'USER','PWD',{host:'127.0.0.1',dialect:"mysql",operatorsAliases:0,timezone:"+05:30"})

这篇关于更改续集时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆