gpt4 book ai didi

node.js - 在 Postgres Node.js 应用程序中将 createAt 和 updatedAt 存储为纪元 unix 时间戳而不是 Date?

转载 作者:行者123 更新时间:2023-12-03 22:35:55 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Sequelize — use UNIX timestamp for DATE fields

(3 个回答)


2年前关闭。




我是 Postgres 的新手。我通过 Sequelize 使用它。我不确定如何在我的 node.js 应用程序中创建模型,以便在我的表中插入行时,将时间指定为纪元 unix 时间戳:

1583855009

代替:
enter image description here

我尝试像这样更改我的模型:
  createdAt: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updatedAt: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
}

但它没有用。使用 pgAdmin 刷新表时,它仍然显示您在此处的图片中看到的内容,而不是像“1583855009”这样的时间戳。

有没有办法代替存储“1583855009”?

最佳答案

存储在 postgres 中的时间戳只是与自纪元时间以来的毫秒数相同的信息的另一种表示形式。

从数据库中提取时间戳字段后, Date.valueOf() 可用于获得所需的数字格式。

下面的片段中的插图:

// replace line below by query retrieving timestamp from the database
let valuefromDB = new Date('2020-03-10T09:36:04.071')

console.log(valuefromDB.valueOf())

关于node.js - 在 Postgres Node.js 应用程序中将 createAt 和 updatedAt 存储为纪元 unix 时间戳而不是 Date?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60621175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com