如何在PostgreSQL中保存JS Date.now()



我尝试使用PostgreSQL timestamp数据类型,但它抛出了一个错误

ERROR:  date/time field value out of range: "1489849402536"

架构

create table times (
  time   timestamp    not null,
);

JS代码

`insert into times(time) values (${Date.now()})`

附言另一种选择是使用bigint但这似乎有点矫枉过正。

使用 to_timestamp() postgres 函数:

`insert into times (time) values (to_timestamp(${Date.now()} / 1000.0))`

或者你可以做类似的事情:

const time = new Date().toISOString();

在查询构建器中:

...
.update()
.set({column_name: time})

以防万一你使用的是Sequelize,你可以使用:sequelize.literal('CURRENT_TIMESTAMP')。例:

await PurchaseModel.update( {purchase_date : sequelize.literal('CURRENT_TIMESTAMP') }, { where: {id: purchaseId} } );

最新更新