我试图使用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日)作为过去的日期。

知道我哪里做错了吗?


当前回答

选择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()">

其他回答

// Returns true if it is today or false if it's not
moment(SpecialToDate).isSame(moment(), 'day');

如果我们想要没有时间的差异,你可以得到不同的日期(只有日期没有时间),如下所示,使用时刻的格式。

因为,我在做的时候遇到了不同的问题;

moment().diff([YOUR DATE])

于是,我想到了下面这些;

const dateValidate = moment(moment().format('YYYY-MM-DD'))。diff(moment([你选择的日期]).format('YYYY-MM-DD'))

IF dateValidate > 0 
   //it's past day
else
   //it's current or future

如果有什么需要改进的地方,请随时评论。

谢谢,

选择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()">

如果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;
  }

最简单的答案是:

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小时时,它也会工作。