javascript - 如何在 Javascript 中使用新日期执行条件?

标签 javascript node.js reactjs react-native

我有一个 curTime 变量,它是使用 new Date() 的当前时间和一个从后端数据调用的 pwChangeDate 值。

  let curTime = new Date();       // Thu Oct 27 2022 15:02:34 GMT+0900
  const pwDate = new Date(pwChangeDate)    // Thu Oct 20 2022 13:51:57 GMT+0900

此时,当 pwDate 基于 curTime 超过 90 天时,我想显示一条提示“90 天已过”。当 83 天过去时,“90 天还剩 7 天”。我想显示一个警报。

但是如果我使用我的代码它不起作用我该如何修复它?

  const pwChangeDate = cookie.get('pwChangeDate');
  const pwDate = new Date(pwChangeDate)

  if (curTime.getDate() >=  pwDate.getDate() - 90) {
    alert('90 days have passed.')
  }

  if (curTime.getDate() >=  pwDate.getDate() - 7) {
    alert('7 days left out of 90 days..')
  }

最佳答案

你可以像这样得到当前数据和 pwDate 之间的差异:

const pwDate = new Date('Thu Oct 20 2022 13:51:57 GMT+0900');
const diff = Math.floor((new Date - pwDate)/1000/60/60/24);

console.log(diff)

关于javascript - 如何在 Javascript 中使用新日期执行条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74217524/

相关文章:

JavaScript:通过局部变量引用宿主函数

javascript - JavaScript 中变量阴影的正确术语是什么?

node.js - Eureka与Node-Js客户端注册成功但心跳失败(总是重新注册)

javascript - useInterval 和 React hooks 的问题 - 无限循环

javascript - onClick() 不会触发 React-Native App 中的函数

javascript - 警告 : Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom

javascript - Request.QueryString 未定义

javascript - 如何将 yield (generators) 与 selenium webdriver promises 一起使用?

node.js - 是否有必要多次要求一个模块(在本例中为 child_process)?

javascript - React Firebase - 未捕获异常 signInWithEmailAndPassword,电子邮件必须是有效字符串