'Change sequelize timezone

I want to make restful app in nodejs

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

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

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"

In database column shows this

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;

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?



Solution 1:[1]

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

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"

Similar to the below code:

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

Solution 2:[2]

Use in Config JSON

"production": {
    "username": "xyz",
    "password": "",
    "database": "dbname",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "port": 123,
    "dialectOptions": {
      "useUTC": false 
    },
    "timezone": "+05:30"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rasik Mahajan
Solution 2 Tyler2P