我已将输入数据以日期格式存储在 postgres 数据库中,但是当我在浏览器中显示日期时,它显示带有时区的日期并将其从 utc 转换。例如,我以
2020-07-16
格式存储日期。但当我显示日期时,它就变成了2020-07-15T18:00:00.000Z
。我尝试使用 select mydate::DATE from table
只获取日期,但它仍然显示带有时区的日期。我在我的节点应用程序中使用node-postgres模块。我怀疑这是node-postgres模块上的一些配置?来自他们的doc:
node-postgres 将 DATE 和 TIMESTAMP 列转换为本地时间 process.env.TZ 处设置的节点进程
我有什么办法可以将其配置为仅解析日期吗?如果我像这样查询
SELECT TO_CHAR(mydate :: DATE, 'yyyy-mm-dd') from table
我得到 2020-07-16
但这只是为了获取日期需要很多工作
您可以制作自己的日期和时间类型解析器:
const pg = require('pg');
pg.types.setTypeParser(1114, function(stringValue) {
return stringValue; //1114 for time without timezone type
});
pg.types.setTypeParser(1082, function(stringValue) {
return stringValue; //1082 for date type
});
类型id可以在文件中找到:node_modules/pg-types/lib/textParsers.js
这里有详细说明:
https://node-postgres.com/features/types
日期/时间戳/时间戳
console.log(result.rows)
// {
// date_col: 2017-05-29T05:00:00.000Z,
// timestamp_col: 2017-05-29T23:18:13.263Z,
// timestamptz_col: 2017-05-29T23:18:13.263Z
// }
bmc=# select * from dates;
date_col | timestamp_col | timestamptz_col
------------+-------------------------+----------------------------
2017-05-29 | 2017-05-29 18:18:13.263 | 2017-05-29 18:18:13.263-05
(1 row)
原因是当你查询日期列时,正是这样;一个约会。
好吧,回到你的问题: 尝试: