无法从 Sequelize 读取 Asia/Calcutta 时区的数据
Cannot read the data in Asia/Calcutta timezone from Sequelize
我正在使用 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
中获取输出,以检查浏览器上是否有任何缓存。没有变化。
我找到问题了,那就是reading the data from the already existing data in the table
。
更恰当地说: 我试图读取我已经存储在数据库中的数据,而不是从 Sequelize
中读取的数据。它总是以 UTC
格式向我显示数据。
我尝试 creating/inserting 使用 Sequelize ORM
的一个数据,在我的 sequelize-db.js
文件 和 boom 中使用上述配置,结果在 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
进行完整操作以查看工作情况:)
我正在使用 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
中获取输出,以检查浏览器上是否有任何缓存。没有变化。
我找到问题了,那就是reading the data from the already existing data in the table
。
更恰当地说: 我试图读取我已经存储在数据库中的数据,而不是从 Sequelize
中读取的数据。它总是以 UTC
格式向我显示数据。
我尝试 creating/inserting 使用 Sequelize ORM
的一个数据,在我的 sequelize-db.js
文件 和 boom 中使用上述配置,结果在 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
进行完整操作以查看工作情况:)