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

编程入门 行业动态 更新时间:2024-10-04 01:15:52

如何将<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点 Date() 插入到 postgresql 时间戳列中?"/>

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

我想同时更新几条记录,不知道怎么办。

我已经尝试过 pg-promise 和 moment 以及单独的节点的各种组合:

myTs = Date();
myTs = moment(new Date()).format("YYYY-MM-DD HH:mm:ss"); 

使用 sql:

UPDATE mytable set ts = $1;
UPDATE mytable set ts = $1::timestamp;

错误:

 time zone "gmt-0800" not recognized
回答如下:

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

假设有一张桌子:

create table test_time (utc_time timestamp);

插入当前UTC时间戳:

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

或者您可以根据您的运行时插入当前

Date
时区:

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

更多推荐

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

本文发布于:2024-05-30 21:55:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   如何将   时间   Date   postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!