【问题标题】:Cannot read the data in Asia/Calcutta timezone from Sequelize无法从 Sequelize 读取亚洲/加尔各答时区的数据
【发布时间】:2021-01-24 04:46:23
【问题描述】:

我将 Postgres 用于我的数据库,并将 Sequelize 作为我的 ORM。我的代码有效,但我在这里看到的唯一问题是,我无法读取 Asia/Kolkata 时区中的数据。每次我尝试以该格式获取数据时,Sequelize 都会向我显示UTC

我尝试了很多东西,但相同的 UTC 时区数据。在我的数据库中,数据以这种格式存储,仅 IST:

|       starttime     |       endtime       |
| 2020-10-07 10:30:00 | 2020-10-07 11:30:00 |

在 Sequelize 输出中,它给了我这个:

{
  "startTime": "2020-10-07T05:00:00.000Z",
  "endTime": "2020-10-07T06:00:00.000Z"
}

1. 我尝试过像这样配置我的 Sequelize 数据库文件:

const sequelize = new Sequelize('dbname', 'username', 'pwd', {
    host: 'localhost',
    dialect: 'postgres',
    dialectOptions: { 
        useUTC: false,
        dateStrings: true,
        typeCast: (field, next) => { // for reading from database
            if (field.type === 'DATETIME') {
                return field.string()
            }
            return next()
        }
    },
    timezone: '+05:30' // for writing the output
});

2.我也试过了,这个

const sequelize = new Sequelize('dbname', 'username', 'pwd', {
    host: 'localhost',
    dialect: 'postgres',
    dialectOptions: { 
        useUTC: false,
        timezone: '+05:30'
    },
    timezone: '+05:30'
});

3.也试过这样做

const sequelize = new Sequelize('dbname', 'username', 'pwd', {
    host: 'localhost',
    dialect: 'postgres',
    dialectOptions: { 
        useUTC: false,
        timezone: 'Asia/Calcutta' // also tried for Asia/Kolkata
    },
    timezone: 'Asia/Calcutta' 
});

现在通过我的所有努力,我仍然可以在 UTC 时区获得时间。我尝试在Incognito mode 中获取输出,以检查浏览器上是否有任何缓存。没有变化。

【问题讨论】:

    标签: node.js postgresql timezone sequelize.js


    【解决方案1】:

    我发现了问题所在,那就是reading the data from the already existing data in the table

    更贴切:我试图读取我已经存储在数据库中的数据,而不是来自Sequelize。它总是以UTC 格式向我显示数据。

    我尝试使用Sequelize ORM 创建/插入一个数据在我的sequelize-db.js 文件中使用上述配置 并且繁荣,结果是Asia/KolkataAsia/Calcutta 格式。虽然,我使用了这个更适合我的配置:

    const sequelize = new Sequelize('dbname', 'username', 'pwd', {
        host: 'localhost',
        dialect: 'postgres',
        dialectOptions: { 
            useUTC: false,
            timezone: '+05:30' // for reading the data
        },
        timezone: '+05:30' // for writing the data
    });
    

    外卖:始终使用Sequelize 进行完整操作以查看工作正常:)

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2021-10-21
      • 2018-01-11
      • 2018-08-31
      • 2012-07-11
      相关资源
      最近更新 更多