我正在使用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/Kolkata
或Asia/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
进行完整操作,以查看工作:)