繁体   English   中英

Nodejs JavaScript,检查两种不同的日期格式是否在24小时内

[英]Nodejs JavaScript, Check if two different date formats are within 24 hours of each other

我正在尝试检查两个日期是否在彼此的24小时内,但是当我记录它们时,格式似乎有所不同。

  static confirmPassword = async (req: Request, res: Response) => {
    const { newPassword, resetPasswordCode } = req.body;
    const account = await userModel.findOne({resetPasswordCode}).exec();
    if (!account) res.status(400).send({ message: 'You have not requested a reset' });

    console.log(account.resetSentAt); // 2019-09-17T04:07:18.634Z
    console.log(Date.now()); // 1568693738552

    res.status(200).send({ message: 'You already confirmed your password' });
  };

Date.now()返回时间戳格式,只需使用Date()new Date() 也许在这种情况下,最好使用一些诸如“ moment.js ”之类的“时间”库,以便您找出两个日期之间的确切时间:

const date1 = moment(account.resetSentAt);
const date2 = moment()
result = date2.diff(date1, 'hours')

暂无
暂无

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

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