在node-postgres模块上显示带有时区的日期类型

Pho*_*Pho 11 postgresql node.js node-postgres

我已将输入数据以日期格式存储在 postgres 数据库中,但是当我在浏览器中显示日期时,它显示带有时区的日期并将其从 utc 转换。例如,我已经以格式存储了日期2020-07-16。但是当我显示日期时,它就变成了2020-07-15T18:00:00.000Z。我尝试使用select mydate::DATE from table仅获取日期,但它仍然显示带有时区的日期。我在我的节点应用程序中使用node-postgres模块。我怀疑这是node-postgres模块上的一些配置?来自他们的文档

node-postgres 将 DATE 和 TIMESTAMP 列转换为在 process.env.TZ 中设置的节点进程的本地时间

他们有什么办法可以将其配置为仅解析日期吗?如果我像这样查询,SELECT TO_CHAR(mydate :: DATE, 'yyyy-mm-dd') from table我会得到2020-07-16,但是为了获取日期需要做很多工作

小智 7

您可以制作自己的日期和时间类型解析器\xef\xbc\x9a

\n

\r\n
\r\n
const pg = require(\'pg\');\npg.types.setTypeParser(1114, function(stringValue) {\n  return stringValue;  //1114 for time without timezone type\n});\n\npg.types.setTypeParser(1082, function(stringValue) {\n  return stringValue;  //1082 for date type\n});
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

类型 id 可以在文件中找到:node_modules/pg-types/lib/textParsers.js

\n


Adr*_*ver 4

这里有详细说明:

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)

Run Code Online (Sandbox Code Playgroud)