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


当前回答

表演

今天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>

铬的结果

其他回答

在javascript中比较日期的最简单方法是首先将其转换为Date对象,然后比较这些日期对象。

下面是一个具有三个功能的对象:

日期.比较(a,b)返回一个数字:-如果a<b,则为1如果a=b,则为0如果a>b,则为1如果a或b是非法日期,则为NaNdates.inRange(d,开始,结束)返回布尔值或NaN:如果d在开始和结束之间(含),则为true如果d在开始之前或结束之后,则为false。如果一个或多个日期是非法的,则为NaN。日期转换由其他函数用于将其输入转换为日期对象。输入可以是日期对象:输入按原样返回。数组:解释为[年,月,日]。注:月份为0-11。数字:解释为自1970年1月1日以来的毫秒数(时间戳)字符串:支持多种不同的格式,如“YYYY/MM/DD”、“MM/DD/YYYY”、“Jan 31 2009”等。对象:解释为具有年、月和日期属性的对象。注:月份为0-11。

.

// Source: http://stackoverflow.com/questions/497790
var dates = {
    convert:function(d) {
        // Converts the date in d to a date-object. The input can be:
        //   a date object: returned without modification
        //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
        //   a number     : Interpreted as number of milliseconds
        //                  since 1 Jan 1970 (a timestamp) 
        //   a string     : Any format supported by the javascript engine, like
        //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
        //  an object     : Interpreted as an object with year, month and date
        //                  attributes.  **NOTE** month is 0-11.
        return (
            d.constructor === Date ? d :
            d.constructor === Array ? new Date(d[0],d[1],d[2]) :
            d.constructor === Number ? new Date(d) :
            d.constructor === String ? new Date(d) :
            typeof d === "object" ? new Date(d.year,d.month,d.date) :
            NaN
        );
    },
    compare:function(a,b) {
        // Compare two dates (could be of any type supported by the convert
        // function above) and returns:
        //  -1 : if a < b
        //   0 : if a = b
        //   1 : if a > b
        // NaN : if a or b is an illegal date
        // NOTE: The code inside isFinite does an assignment (=).
        return (
            isFinite(a=this.convert(a).valueOf()) &&
            isFinite(b=this.convert(b).valueOf()) ?
            (a>b)-(a<b) :
            NaN
        );
    },
    inRange:function(d,start,end) {
        // Checks if date in d is between dates in start and end.
        // Returns a boolean or NaN:
        //    true  : if d is between start and end (inclusive)
        //    false : if d is before start or after end
        //    NaN   : if one or more of the dates is illegal.
        // NOTE: The code inside isFinite does an assignment (=).
       return (
            isFinite(d=this.convert(d).valueOf()) &&
            isFinite(start=this.convert(start).valueOf()) &&
            isFinite(end=this.convert(end).valueOf()) ?
            start <= d && d <= end :
            NaN
        );
    }
}

与往常一样比较<和>,但涉及==或==的任何内容都应使用+前缀。像这样:

const x=新日期(‘2013-05-23’);const y=新日期(‘2013-05-23’);//小于、大于即可:console.log('x<y',x<y);//假的console.log('x>y',x>y);//假的console.log('x<=y',x<=y);//真的console.log('x>=y',x>=y);//真的console.log('x===y',x===y);//假的,哎呀!//任何涉及“==”或“===”的内容都应使用“+”前缀//然后将比较日期的毫秒值console.log('+x===+y',+x===+y);//真的

简单的方法是,

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

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

我对这个问题的简单回答

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

为了从JavaScript中的自由文本创建日期,您需要将其解析为Date对象。

您可以使用Date.parse(),它获取自由文本并尝试将其转换为新的日期,但如果您能够控制页面,我建议使用HTML选择框或日期选择器,例如YUI日历控件或jQueryUI日期选择器。

正如其他人所指出的,一旦你有了日期,你就可以使用简单的算术来减去日期,并将其转换为天数,方法是将数字(以秒为单位)除以一天中的秒数(60*60*24=86400)。