【问题标题】:Moment: the user should be able to perform X as long as the current time and date is max 30 minutes before the requested time时刻:只要当前时间和日期在请求时间之前最多 30 分钟,用户就应该能够执行 X
【发布时间】:2020-04-06 20:37:05
【问题描述】:

我试过这样做

const currentTime = moment(moment().format('MMMM DD YYYY, h:mm a'));
        const timeDifference = moment.duration(currentTime.diff(details.requestedStartTimestamp)).asMinutes();

但我很困惑,因为如果日期是今天或未来日期,timeDifference 显示负数,如果是过去日期,则显示正数,这迫使我添加这个条件:

if(timeDifference

【问题讨论】:

    标签: javascript reactjs time momentjs


    【解决方案1】:

    time1.diff(time2) 表示time1 - time2,因此得到未来日期的否定结果是正确的。

    附带说明,您现在不需要格式化时间来获取moment 对象。

    为了得到你想要的结果,这段代码可以工作:

    const now = moment();
    const requestedTime = moment(details.requestedStartTimestamp);
    const diff = requestedTime.diff(now, 'minutes');
    
    if (diff >= 30) {
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2021-03-14
      • 1970-01-01
      • 2011-12-10
      相关资源
      最近更新 更多