我试图使用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()">

其他回答

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

你可以使用momentjs的isAfter()查询函数:

检查一个时刻是否在另一个时刻之后。

moment('2010-10-20').isAfter('2010-10-19'); // true

如果希望将粒度限制为毫秒以外的单位,请将单位作为第二个参数传递。

moment('2010-10-20').isAfter('2010-01-01', 'year'); // false

moment('2010-10-20').isAfter('2009-12-31', 'year'); // true

http://momentjs.com/docs/#/query/is-after/

要查看今天是否有:

如果我们比较两个同时包含时间信息的日期,那么isSame显然会失败。如果两个日期跨越了新的一天,Diff将失败:

var date1 = moment("01.01.2016 23:59:00", "DD.MM.YYYY HH.mm.ss");
var date2 = moment("02.01.2016 00:01:00", "DD.MM.YYYY HH.mm.ss");
var diff = date2.diff(date1); // 2seconds

我认为最好的方法,即使它不是快速和简短的,是以下:

var isSame = date1.date() == date2.date() && date1.month() == date2.month() && date1.year() == date2.year()

检查它是否在将来:

正如其他用户所建议的那样,diff方法是有效的。

var isFuture = now.diff(anotherDate) < 0 
function isTodayOrFuture(date){
  date = stripTime(date);
  return date.diff(stripTime(moment.now())) >= 0;
}

function stripTime(date){
  date = moment(date);
  date.hours(0);
  date.minutes(0);
  date.seconds(0);
  date.milliseconds(0);
  return date;
}

然后用它来换行

isTodayOrFuture(YOUR_TEST_DATE_HERE)

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