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


当前回答

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

/* 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));

其他回答

var curDate=new Date();
var startDate=document.forms[0].m_strStartDate;

var endDate=document.forms[0].m_strEndDate;
var startDateVal=startDate.value.split('-');
var endDateVal=endDate.value.split('-');
var firstDate=new Date();
firstDate.setFullYear(startDateVal[2], (startDateVal[1] - 1), startDateVal[0]);

var secondDate=new Date();
secondDate.setFullYear(endDateVal[2], (endDateVal[1] - 1), endDateVal[0]);
if(firstDate > curDate) {
    alert("Start date cannot be greater than current date!");
    return false;
}
if (firstDate > secondDate) {
    alert("Start date cannot be greater!");
    return false;
}

我对这个问题的简单回答

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

表演

今天2020.02.27我在MacOs High Sierra v10.13.6上对Chrome v80.0、Safari v13.0.5和Firefox 73.0.1上选择的解决方案进行了测试

结论

所有浏览器的解决方案d1==d2(D)和d1==d2(E)最快解决方案getTime(A)比valueOf(B)快(两者都是中速)解决方案F、L、N对于所有浏览器来说都是最慢的

细节

下面的代码片段中介绍了性能测试中使用的解决方案。您可以在您的机器上执行测试

函数A(d1,d2){return d1.getTime()==d2.getTime);}函数B(d1,d2){返回d1.valueOf()==d2.valueOf();}函数C(d1,d2){返回数字(d1)==数字(d2);}函数D(d1,d2){返回d1==d2;}函数E(d1,d2){返回d1==d2;}函数F(d1,d2){返回(!(d1>d2||d2>d1));}函数G(d1,d2){返回d1*1==d2*1;}函数H(d1,d2){返回+d1==+d2;}函数I(d1,d2){回来(+d1-+d2);}函数J(d1,d2){回来(d1-d2);}函数K(d1,d2){返回d1-d2==0;}函数L(d1,d2){回来((d1>d2)-(d1<d2));}函数M(d1,d2){return d1.getFullYear()==d2.getFullYear()&&d1.getDate()==d2.getDate&&d1.getMonth()==d2.getMont();}函数N(d1,d2){return(isFinite(d1.valueOf())&&isFinite!((d1>d2)-(d1<d2)):假);}//测试let过去=新日期('2002-12-24');//过去的let now=新日期('2020-02-26');//现在console.log('代码d1>d2 d1<d2 d1=d2')var log=(l,f)=>console.log(`${l}${f(现在,过去)}${f(过去,现在)}${f(现在、现在)}`);日志('A',A);日志('B',B);日志('C',C);日志('D',D);对数('E',E);日志('G',G);对数('H',H);日志('I',I);日志('J',J);log('K',K);对数('L',L);日志(M’,M);对数('N',N);p{颜色:红色}<p>此代码段仅显示已测试的解决方案(它本身不执行测试)</p>

铬的结果

假设你得到了日期对象A和B,得到它们的EPOC时间值,然后减去以毫秒为单位的差值。

var diff = +A - +B;

这就是全部。

以上给出的所有答案只解决了一件事:比较两个日期。

事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:

如果我想检查一个人是否满18岁怎么办?

不幸的是,上面给出的答案中没有一个能够回答这个问题。

例如,当前时间(大约是我开始键入这些单词的时间)是2020年1月31日星期五10:41:04 GMT-0600(中部标准时间),而客户将其出生日期输入为“01/31/2002”。

如果我们使用“365天/年”,即“31536000000”毫秒,我们将得到以下结果:

       let currentTime = new Date();
       let customerTime = new Date(2002, 1, 31);
       let age = (currentTime.getTime() - customerTime.getTime()) / 31536000000
       console.log("age: ", age);

打印如下:

       age: 17.92724710838407

但从法律上讲,这位客户已经18岁了。即使他输入“01/30/2002”,结果仍然是

       age: 17.930039743467784

其小于18。系统将报告“未成年”错误。

“01/29/2002”、“01/28/2002”和“01/27/2002”。。。“01/05/2002”,直到“01/04/2002”。

这样的系统只会杀死所有出生在18岁0天到18岁26天之间的客户,因为他们在法律上是18岁,而系统显示“未成年”。

以下是对类似问题的回答:

invalidBirthDate: 'Invalid date. YEAR cannot be before 1900.',
invalidAge: 'Invalid age. AGE cannot be less than 18.',

public static birthDateValidator(control: any): any {
    const val = control.value;
    if (val != null) {
        const slashSplit = val.split('-');
        if (slashSplit.length === 3) {
            const customerYear = parseInt(slashSplit[0], 10);
            const customerMonth = parseInt(slashSplit[1], 10);
            const customerDate = parseInt(slashSplit[2], 10);
            if (customerYear < 1900) {
                return { invalidBirthDate: true };
            } else {
                const currentTime = new Date();
                const currentYear = currentTime.getFullYear();
                const currentMonth = currentTime.getMonth() + 1;
                const currentDate = currentTime.getDate();
                if (currentYear - customerYear < 18) {
                    return { invalidAge: true };
                } else if (
                    currentYear - customerYear === 18 &&
                    currentMonth - customerMonth < 0) {
                    return { invalidAge: true };
                } else if (
                    currentYear - customerYear === 18 &&
                    currentMonth - customerMonth === 0 &&
                    currentDate - customerDate < 0) {
                    return { invalidAge: true };
                } else {
                    return null;
                }
            }
        }
    }
}