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


当前回答

尝试使用此代码

var f =date1.split("/");

var t =date2.split("/");

var x =parseInt(f[2]+f[1]+f[0]);

var y =parseInt(t[2]+t[1]+t[0]);

if(x > y){
    alert("date1 is after date2");
}

else if(x < y){
    alert("date1 is before date2");
}

else{
    alert("both date are same");
}

其他回答

在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
        );
    }
}

在JavaScript中比较日期非常简单。。。JavaScript内置了日期比较系统,因此比较起来非常简单。。。

只需按照以下步骤比较两个日期值,例如,您有两个输入,每个输入都有一个字符串中的日期值,您可以比较它们。。。

1.您有两个从输入中获得的字符串值,您希望对它们进行比较,如下所示:

var date1 = '01/12/2018';
var date2 = '12/12/2018';

2.它们需要是日期对象,以便作为日期值进行比较,所以只需使用new Date()将它们转换为日期,我只需重新分配它们以简化解释,但您可以按照自己的意愿进行操作:

date1 = new Date(date1);
date2 = new Date(date2);

3.现在只需使用><>=<=

date1 > date2;  //false
date1 < date2;  //true
date1 >= date2; //false
date1 <= date2; //true

到目前为止,最简单的方法是从另一个日期中减去一个日期并比较结果。

var oDateOne=新日期();var oDateTwo=新日期();警报(oDateOne-oDateTwo==0);警报(oDateOne-oDateTwo<0);警报(oDateOne-oDateTwo>0);

注意-仅比较日期部分:

当我们在javascript中比较两个日期时。这需要数小时、数分钟和数秒的时间。。因此,如果我们只需要比较日期,这是一种方法:

var date1= new Date("01/01/2014").setHours(0,0,0,0);

var date2= new Date("01/01/2014").setHours(0,0,0,0);

现在:如果date1.valueOf()>date2.valueOf。

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

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);//真的