繁体   English   中英

书架/ knex增加时间戳记时间

[英]bookshelf/knex add time to timestamp

使用NodeJS,Knex,Bookshelf JS ORM和PostgreSQL,我有一个字段表table.timestamp('reset_password_expires'); -如何设定未来的日期(假设现在是10分钟)?

我试过了:

new User.User({'email': email}).fetch().then((user) => {
      user.set('reset_password_expires', Date.now() + 60000);
      user.save();
      ...

但这引发了:

未处理的拒绝错误:日期/时间字段值超出范围:“ 1544577763253”

也尝试过

user.set('reset_password_expires', 'NOW()' + '10 minutes'::interval);

但这是无效的语法。

我也尝试将字段类型更改为dateTime ,结果相同。

我认为PostgreSQL的要求时间戳是在普通的日期时间格式(如描述在这里 )。 目前,您距纪元已过去了毫秒。

在开发人员工具中:

> Date.now() + 60000
1544706877041

您可以尝试使用以下方法:

new Date((new Date()).getTime() + 60000)

在开发人员工具中:

> new Date((new Date()).getTime() + 60000)
Thu Dec 13 2018 14:15:50 GMT+0100 (Central European Standard Time)

这是我自己使用的实用程序功能:

module.exports.inHours = (hours) => {
  const minutes = hours * 60;
  const seconds = minutes * 60;
  const milliseconds = seconds * 1000;

  return new Date((new Date()).getTime() + milliseconds);
};

我的代码看起来与您的代码略有不同,但这就是我调用该函数的方式:

await userQueries.update(user.user_id, {
  resetPasswordToken: resetToken,
  resetPasswordExpires: inHours(1),
  modified_at: new Date(),
});

这是日期时间,但是不是时间戳。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM