如何在 PostgreSQL 中保存 JS Date.now()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42876071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save JS Date.now() in PostgreSQL?
提问by Alexey Petrushin
I tried to use PostgreSQL timestamp
datatype, but it throws an error
我尝试使用 PostgreSQLtimestamp
数据类型,但它引发错误
ERROR: date/time field value out of range: "1489849402536"
The schema
架构
create table times (
time timestamp not null,
);
JS code
JS代码
`insert into times(time) values (${Date.now()})`
P.S. another option is to use bigint
but it seems like an overkill.
PS 另一种选择是使用,bigint
但这似乎有点矫枉过正。
回答by Udi
Use the to_timestamp()
postgres function:
`insert into times (time) values (to_timestamp(${Date.now()} / 1000.0))`
回答by Arthur Mastropietro
Just in case you are using Sequelize, you can use: sequelize.literal('CURRENT_TIMESTAMP')
.
Example:
以防万一您使用 Sequelize,您可以使用:sequelize.literal('CURRENT_TIMESTAMP')
。例子:
await PurchaseModel.update( {purchase_date : sequelize.literal('CURRENT_TIMESTAMP') }, { where: {id: purchaseId} } );
回答by Anton Kornus
Or you can do something similar to this:
或者你可以做类似的事情:
const time = new Date().toISOString();
In your query builder:
在您的查询构建器中:
...
.update()
.set({column_name: time})