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


当前回答

您可以使用以下代码:

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文章。

其他回答

“一些”发布的代码的改进版本

/* Compare the current date against another date.
 *
 * @param b  {Date} the other date
 * @returns   -1 : if this < b
 *             0 : if this === b
 *             1 : if this > b
 *            NaN : if a or b is an illegal date
*/ 
Date.prototype.compare = function(b) {
  if (b.constructor !== Date) {
    throw "invalid_date";
  }

 return (isFinite(this.valueOf()) && isFinite(b.valueOf()) ? 
          (this>b)-(this<b) : NaN 
        );
};

用法:

  var a = new Date(2011, 1-1, 1);
  var b = new Date(2011, 1-1, 1);
  var c = new Date(2011, 1-1, 31);
  var d = new Date(2011, 1-1, 31);

  assertEquals( 0, a.compare(b));
  assertEquals( 0, b.compare(a));
  assertEquals(-1, a.compare(c));
  assertEquals( 1, c.compare(a));

我对这个问题的简单回答

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

Date对象将执行您想要的操作-为每个日期构造一个,然后使用>、<、<=或>=对它们进行比较。

==,!=,==,和!==运算符要求您使用date.getTime(),如中所示

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

明确地说,直接检查日期对象是否相等是行不通的

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

不过,我建议您使用下拉列表或类似的受约束的日期输入形式,而不是文本框,以免您陷入输入验证地狱。


对于好奇的date.getTime()文档:

返回指定日期的数值,即自1970年1月1日00:00:00 UTC以来的毫秒数。(之前的时间返回负值。)

简单的方法是,

var first = '2012-11-21';
var second = '2012-11-03';

if (new Date(first) > new Date(second) {
    .....
}

仅比较日期(忽略时间分量):

Date.prototype.sameDay = function(d) {
  return this.getFullYear() === d.getFullYear()
    && this.getDate() === d.getDate()
    && this.getMonth() === d.getMonth();
}

用法:

if(date1.sameDay(date2)) {
    // highlight day on calendar or something else clever
}

我不再建议修改内置对象的原型。请尝试以下操作:

函数为SameDay(d1,d2){return d1.getFullYear()==d2.getFullYear()&&d1.getDate()==d2.getDate&&d1.getMonth()==d2.getMont();}console.log(isSameDay(新日期(‘2021 1月15日02:39:53 GMT-0800’),新日期(’2021 1月15号23:39:53格林威治时间-0800’));console.log(isSameDay(新日期(2021 1月15日10:39:53 GMT-0800),新日期(2021 1月16日10:39:33 GMT-0800'));

注意:将返回您所在时区的年/月/日;如果您想检查两个日期是否在不同时区的同一天,我建议使用时区感知库。

e.g.

> (new Date('Jan 15 2021 01:39:53 Z')).getDate()  // Jan 15 in UTC
14  // Returns "14" because I'm in GMT-08