如何将节点 Date() 插入到 postgresql 时间戳列中?

Coc*_*oco 7 postgresql timestamp date node.js momentjs

我想同时更新多条记录,但我不知道该怎么做。

我已经尝试了 pg-promise 和 moment 以及 node 的各种组合:

myTs = Date();
myTs = moment(new Date()).format("YYYY-MM-DD HH:mm:ss"); 
Run Code Online (Sandbox Code Playgroud)

用sql:

UPDATE mytable set ts = $1;
UPDATE mytable set ts = $1::timestamp;
Run Code Online (Sandbox Code Playgroud)

错误:

 time zone "gmt-0800" not recognized
Run Code Online (Sandbox Code Playgroud)

Alb*_*ina 1

最好以没有时区的 UTC 格式将时间戳存储在数据库中。

假设有一个表(PostgreSQL):

create table test_time (utc_time timestamp);
Run Code Online (Sandbox Code Playgroud)

要插入当前 UTC 时间戳:

const res = await db.query({
  text: "insert into test_time (utc_time) values ($1) returning *",
  values: [new Date(new Date().toISOString())],
});
Run Code Online (Sandbox Code Playgroud)

或者您可以Date根据您的运行时间插入当前的时区:

const res = await db.query({
  text: "insert into test (date) values ($1) returning *",
  values: [new Date()],
});
Run Code Online (Sandbox Code Playgroud)