我试图使用momentjs来检查给定的日期是今天还是将来。

这是我目前所拥有的:

<script type="text/javascript" src="http://momentjs.com/downloads/moment.min.js"></script>
<script type="text/javascript">

var SpecialToDate = '31/01/2014'; // DD/MM/YYYY

var SpecialTo = moment(SpecialToDate, "DD/MM/YYYY");
if (moment().diff(SpecialTo) > 0) {
    alert('date is today or in future');
} else {
    alert('date is in the past');
}

</script>

代码评估我的日期(2014年1月31日)作为过去的日期。

知道我哪里做错了吗?


当前回答

最简单的答案是:

const firstDate = moment('2020/10/14'); // the date to be checked
const secondDate = moment('2020/10/15'); // the date to be checked

firstDate.startOf('day').diff(secondDate.startOf('day'), 'days'); // result = -1
secondDate.startOf('day').diff(firstDate.startOf('day'), 'days'); // result = 1

它将检查midnight值并返回准确的结果。当两个日期之间的时间差小于24小时时,它也会工作。

其他回答

invert is before方法检查日期是否与今天或未来相同,如下所示:

!moment(yourDate).isBefore(moment(), "day");

在阅读文档:http://momentjs.com/docs/#/displaying/difference/之后,您必须将diff函数视为减号运算符。

                   // today < future (31/01/2014)
today.diff(future) // today - future < 0
future.diff(today) // future - today > 0

因此,你必须扭转你的状况。

如果你想检查是否一切正常,你可以在函数中添加一个额外的参数:

moment().diff(SpecialTo, 'days') // -8 (days)

用最简单的一个来检查未来的日期

if(moment().diff(yourDate) >=  0)
     alert ("Past or current date");
else
     alert("It is a future date");

如果firstDate相同或在(future) secondDate之后返回true否则返回false。Toda是firstDate = new Date();

  static isFirstDateSameOrAfterSecondDate(firstDate: Date, secondDate: Date): boolean {
    var date1 = moment(firstDate);
    var date2 = moment(secondDate);
    if(date1 && date2){
      return date1.isSameOrBefore(date2,'day');
    }
    return false;
  }

有isSame, isBefore和isAfter为日比较时刻的例子;

  static isFirstDateSameSecondDate(firstDate: Date, secondDate: Date): boolean {
    var date1 = moment(firstDate);
    var date2 = moment(secondDate);
    if (date1 && date2) {
      return date1.isSame(date2,'day');
    }
    return false;
  }

  static isFirstDateAfterSecondDate(firstDate: Date, secondDate: Date): boolean {
    var date1 = moment(firstDate);
    var date2 = moment(secondDate);
    if(date1 && date2){
      return date1.isAfter(date2,'day');
    }
    return false;
  }

  static isFirstDateBeforeSecondDate(firstDate: Date, secondDate: Date): boolean {
    var date1 = moment(firstDate);
    var date2 = moment(secondDate);
    if(date1 && date2){
      return date1.isBefore(date2,'day');
    }
    return false;
  }

选择yesterday,在moment()的帮助下检查过去的天数。减去(1天);

Reference:- http://momentjs.com/docs/#/manipulating/subtract/ function myFunction() { var yesterday = moment().subtract(1, "day").format("YYYY-MM-DD"); var SpecialToDate = document.getElementById("theDate").value; if (moment(SpecialToDate, "YYYY-MM-DD", true).isAfter(yesterday)) { alert("date is today or in future"); console.log("date is today or in future"); } else { alert("date is in the past"); console.log("date is in the past"); } } <script src="http://momentjs.com/downloads/moment.js"></script> <input type="date" id="theDate" onchange="myFunction()">