使用 JavaScript比较两个日期

有人可以建议一种方法来比较两个日期的值大于,小于,而不是过去使用 JavaScript?

有人可以建议一种方法来比较两个日期的值大于,小于,而不是过去使用 JavaScript?

3120

Date object将做你想要的-为每个日期构建一个,然后使用><<=>=

==!====!==运算符要求使用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() documentation

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

495

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

const x = new Date('2013-05-23');
const y = new Date('2013-05-23');
// less than, greater than is fine:
console.log('x < y', x < y); // false
console.log('x > y', x > y); // false
console.log('x <= y', x <= y); // true
console.log('x >= y', x >= y); // true
console.log('x === y', x === y); // false, oops!
// anything involving '==' or '===' should use the '+' prefix
// it will then compare the dates' millisecond values
console.log('+x === +y', +x === +y); // true
487

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

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

日期.比较 (a,b)

返回一个数字:

-1 如果 a & lt;b

0 如果 a = b

1 如果 a & gt;b

NaN 如果 a 或 b 是非法日期

dates.inRange(d,start,end)

返回布尔值或 NaN:

true如果d介于startend之间(含)

false如果dstart之前或end之后。

如果一个或多个日期是非法的,则为 NaN。

dates.convert

其他函数用于将其输入转换为日期对象。输入可以是

adate-object:按原样返回输入。

数组:解释为 [年,月,日]。月为 0-11。

anumber:解释为自 1970 年 1 月 1 日以来的毫秒数(时间戳)

astring:支持几种不同的格式,如“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 ignment (=).
        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 ignment (=).
       return (
            isFinite(d=this.convert(d).valueOf()) &&
            isFinite(start=this.convert(start).valueOf()) &&
            isFinite(end=this.convert(end).valueOf()) ?
            start <= d && d <= end :
            NaN
        );
    }
}
198

关系运算符<<=>>=可用于比较 JavaScript 日期:

var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 2);
d1 <  d2; // true
d1 <= d2; // true
d1 >  d2; // false
d1 >= d2; // false

但是,等式运算符==!====!==不能用于比较日期because(的值):

对于严格或抽象比较,两个不同的对象永远不相等。

仅当操作数引用相同的 Object 时,比较 Objects 的表达式才为 true。

您可以使用以下任何一种方法比较日期的值是否相等:

var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 1);
/*
 * note: d1 == d2 returns false as described above
 */
d1.getTime() == d2.getTime(); // true
d1.valueOf() == d2.valueOf(); // true
Number(d1)   == Number(d2);   // true
+d1          == +d2;          // true

Date.getTime()Date.valueOf()都返回自 1970 年 1 月 1 日 00:00 UTC 以来的毫秒数。Number函数和一元+运算符都在后台调用valueOf()方法。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(589)
intellij想法设置空白节点.js项目
上一篇
nodejs应用程序的弹性 apm中没有数据
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(21条)