有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。


当前回答

试试这个而比较日期应为iso格式“yyyy-MM-dd”如果要仅比较日期,请使用此datehelper

<a href="https://plnkr.co/edit/9N8ZcC?p=preview"> Live Demo</a>

其他回答

您可以使用以下代码:

var firstValue = "2012-05-12".split('-');
var secondValue = "2014-07-12".split('-');

 var firstDate=new Date();
 firstDate.setFullYear(firstValue[0],(firstValue[1] - 1 ),firstValue[2]);

 var secondDate=new Date();
 secondDate.setFullYear(secondValue[0],(secondValue[1] - 1 ),secondValue[2]);     

  if (firstDate > secondDate)
  {
   alert("First Date  is greater than Second Date");
  }
 else
  {
    alert("Second Date  is greater than First Date");
  }

还要检查Date类的MDN文章。

我对这个问题的简单回答

checkDisabled(date) {
    const today = new Date()
    const newDate = new Date(date._d)
    if (today.getTime() > newDate.getTime()) {
        return true
    }
    return false
}
       

通过Moment.js

Jsfidle:http://jsfiddle.net/guhokemk/1/

function compare(dateTimeA, dateTimeB) {
    var momentA = moment(dateTimeA,"DD/MM/YYYY");
    var momentB = moment(dateTimeB,"DD/MM/YYYY");
    if (momentA > momentB) return 1;
    else if (momentA < momentB) return -1;
    else return 0;
}

alert(compare("11/07/2015", "10/07/2015"));

如果dateTimeA大于dateTimeB,则该方法返回1

如果dateTimeA等于dateTimeB,则该方法返回0

如果dateTimeA小于dateTimeB,则该方法返回-1

假设您处理的是2014年[:-/.]06[:-//.]06或2014年06月[:-/./.]06日格式,那么您可以这样比较日期

var a = '2014.06/07', b = '2014-06.07', c = '07-06/2014', d = '07/06.2014';

parseInt(a.replace(/[:\s\/\.-]/g, '')) == parseInt(b.replace(/[:\s\/\.-]/g, '')); // true
parseInt(c.replace(/[:\s\/\.-]/g, '')) == parseInt(d.replace(/[:\s\/\.-]/g, '')); // true
parseInt(a.replace(/[:\s\/\.-]/g, '')) < parseInt(b.replace(/[:\s\/\.-]/g, '')); // false
parseInt(c.replace(/[:\s\/\.-]/g, '')) > parseInt(d.replace(/[:\s\/\.-]/g, '')); // false

如您所见,我们去掉分隔符,然后比较整数。

function datesEqual(a, b)
{
   return (!(a>b || b>a))
}