无法从 Sequelize 读取亚洲/加尔各答时区的数据



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

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

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

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

{
"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中获取输出,以检查浏览器上是否有任何缓存。没有变化。

我已经解决了问题所在,这就是reading the data from the already existing data in the table.

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

我尝试在我的sequelize-db.js文件和 boom 中使用具有上述配置的Sequelize ORM创建/插入一个数据,结果是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进行完整操作,以查看工作:)

最新更新