【问题标题】:PostgreSQL outputs different TimeStamp format than Javascript TimeStamp formatPostgreSQL 输出的时间戳格式与 Javascript 时间戳格式不同
【发布时间】:2020-04-09 07:59:20
【问题描述】:

我无法将 SQL 的结果绑定到我在 Javascript /Typescript 中的模型。

在我的模型中,我的属性为 created_at,类型为 Date

当我在 Postgres SQL 语句中使用 JSON 函数来避免关系的父行重复时,我会得到不同格式的时间戳。

这是一个简单的例子

SELECT
   a.*,
   (
      SELECT
         ROW_TO_JSON(profile) 
      FROM
         (
            SELECT
               *
            FROM
               profile p 
            WHERE
               p.account_id = a.id 
         )
         profile 
   )
   AS profile 
FROM
   account a 
WHERE
   a.id = 16

这是 JSON 格式的结果

{
   "id":16,
   "email":"test@gmail.com",
   "password":"$password",
   "role_id":0,
   "created_at":"2020-04-01T22:03:44.324Z",
   "profile":{
      "id":8,
      "username":"firmanjml",
      "gender":0,
      "bio":null,
      "avatar":"www.firmanjml.me/test.jpg",
      "account_id":16,
      "created_at":"2020-04-02T06:03:44.32498"
   }
}

我注意到来自 account 表的父行在 created_at 的末尾有 Z,而转换为 JSON 的子表具有不同的时间戳格式。

有没有办法可以让所有时间戳都采用Javascript 格式?

查询以创建架构和插入数据

CREATE TABLE "account"(
    id SERIAL primary key,
    email varchar(50) not null,
    password varchar(50) not null,
    role_id int2 default 0 not null, 
    created_at timestamp default now() not null
);

CREATE TABLE "profile"(
    id SERIAL primary key,
    username varchar(50) not null,
    gender int2 not null,
    bio varchar(50),
    avatar varchar(50),
    account_id integer not null REFERENCES account (id),
    created_at timestamp default now() not null
);

INSERT INTO "account" (email,"password","role_id",created_at) VALUES 
('test@gmail.com','$password',0,'2020-04-02 06:03:44.324');

INSERT INTO "profile" (username,gender,bio,avatar,account_id,created_at) VALUES 
('fimrnajml',0,NULL,'www.firmanjml.me/test.jpg',1,'2020-04-02 06:03:44.324');

【问题讨论】:

  • 根据我的定义,JS 时间戳是错误的。 'Z' 是一个时区,除非你在任何地方都使用它,否则你应该这样标记它。
  • 您好,感谢您的回复。如果时间戳带有 Z,我的打字稿模型只能绑定行数据,created_at 的其余部分会给我一个错误。我希望所有时间戳都有“Z”符号。如果没有 Z,它将检测为无效的 DateTime 格式。

标签: javascript node.js postgresql timestamp pg


【解决方案1】:

使用 TO_CHAR() 函数来格式化 SQL 中的时间戳,例如 https://www.postgresqltutorial.com/postgresql-to_char/

'YYYY-MM-DD"T"HH24:MI:SS.US"Z"' 的格式应该可以做到。这假设您所有的时间戳都是 UTC(专业人士的做法:-)

您的 SQL 如下所示:

SELECT
   a.*,
   (
      SELECT
         ROW_TO_JSON(profile) 
      FROM
         (
            SELECT
               username,gender,bio,avatar,account_id,to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') created_at
            FROM
               profile p 
            WHERE
               p.account_id = a.id 
         )
         profile 
   )
   AS profile 
FROM
   account a
WHERE
   a.id = 16

试试here

【讨论】:

  • 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"' 已修复。谢谢!
  • Yes .MS"Z" 解决了这个问题。为什么转换为 JSON 会在最后去掉“Z”?默认情况下,Postgres 以“Z”作为常规 UTC 时间戳发送出去。
  • “Z”将时间戳的时区显式设置为 UTC。默认行为根本不设置时区,PostgreSQL 假设系统时区是正确的(我想,已经有一段时间了)。见en.wikipedia.org/wiki/ISO_8601#Time_zone_designators
猜你喜欢
  • 1970-01-01
  • 2020-05-23
  • 2011-12-04
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 2016-07-12
  • 2019-02-17
相关资源
最近更新 更多