比较postgresql中的日期时间

compare datetime in postgresql

为了重设密码,我正在检查令牌是否过期,有效期为一个小时。 为此,我将 'inOneHour' 日期时间保存在 'resetPasswordExpires' 列 (db) 中,并在重置密码请求时与 'now' 日期时间进行比较。但它失败了。

moment.js

export const now = moment().format();
export const inOneHour = moment()
  .add(1, 'hours')
  .format();

postgresql

table.datetime('resetPasswordExpires', { useTz: false });

auth.js

exports.forgotPassword = (req, res) => {
const { username, email, inOneHour } = req.body;

  return knex('users')
    .where({ username, email })
    .first()
    .then(async user => {
        ...
        return knex('users')
          .where({ username })
          .first()
          .update({
            resetPasswordToken: token,
            resetPasswordExpires: inOneHour,
          })
          .then(() => {
            ...
    });
};

exports.resetPasswordWithToken = (req, res) => {
  const { newPassword, now } = req.body;
  const { token } = req.params;

  knex('users')
    .where({ resetPasswordToken: token })
    .andWhere('resetPasswordExpires', '>', now)
    .first()
    .then(user => {
      if (user) {
        const id = user.id;
        return setPassword(id, newPassword)
          .then(() =>
            knex('users')
              .where({
                id,
              })
              .first()
              .update({
                resetPasswordToken: null,
                resetPasswordExpires: null,
              }),
          )
          .then(() => res.status(200).json())
          .catch(err => res.status(409).json(err));
      }
      return res.status(409).json('Access token is invalid or has expired');
    })
    .catch(err => res.status(409).json(err));
};

当 moment.format()

// passing datetime to update 'resetPasswordExpires' column
console.log(inOneHour) // 2019-07-19T01:19:16+09:00

// 'resetPasswordExpires' value after updated
console.log(resetPasswordExpires) // 2019-07-18T16:19:16.000Z

// passing datetime to compare
console.log(now) // 2019-07-19T00:20:02+09:00

当 moment.format('MMMM DD YYYY, HH:mm:ss')

console.log(inOneHour) // July 19 2019, 01:16:17
console.log(resetPasswordExpires) // 2019-07-18T16:16:17.000Z
console.log(now) // July 19 2019, 00:17:38

两种不同的日期时间格式以相同的格式保存在数据库中。 即使当我使用时区设置 'resetPasswordExpired' 日期时间类型并使用 moment.format() 时,它仍然不起作用。 我该如何解决这个问题?

当moment.format() && table.datetime('resetPasswordExpires');

console.log(inOneHour) // July 19 2019, 01:16:17
console.log(resetPasswordExpires) // 2019-07-18T16:16:17.000Z
console.log(now) // July 19 2019, 00:17:38

我重新打开笔记本电脑。不知何故,这段代码现在可以工作了;; 但是我意识到我实际上不需要在数据库中保存令牌的过期时间,因为 JWT 令牌已经生成了过期时间。