首页 > JavaScript > 可以一种方法来比较过去大于、小于和过去不同的两个日期的值吗?

可以一种方法来比较过去大于、小于和过去不同的两个日期的值吗?

上一篇 下一篇

有人可以建议一种方法来比较过去大于、小于和过去不同的两个日期的值吗?这些值将来自文本框。

分割线

网友回答:

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

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

分割线

网友回答:

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()

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

分割线

网友回答:

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

下面是一个具有三个函数的对象:

  • 日期比较(a,b)
    返回一个数字:

    • -1 如果 a < b
    • 如果 a = b 则为 0
    • 1 如果 a > b
    • 如果 a 或 b 是非法日期,则为 NaN
  • 日期.在范围 (d,开始,结束)
    返回布尔值或 NaN:

    • 如果 d开始结束之间(包括 D),则为 true
    • 如果 d开始之前或结束之后,则为 false
    • NaN,如果一个或多个日期是非法的。
  • 日期.转换
    由其他函数用于将其输入转换为日期对象。输入可以是

    • 日期对象:输入按原样返回。
    • 数组:解释为 [年,月,日]。注意月份是0-11。
    • 一个数字:解释为自 1 年 1970 月 <> 日以来的毫秒数(时间戳)
    • 字符串:支持几种不同的格式,如“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
        );
    }
}

模板简介:该模板名称为【可以一种方法来比较过去大于、小于和过去不同的两个日期的值吗?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【JavaScript】栏目查找您需要的精美模板。

相关搜索
  • 下载密码 lanrenmb
  • 下载次数 207次
  • 使用软件 Sublime/Dreamweaver/HBuilder
  • 文件格式 编程语言
  • 文件大小 暂无信息
  • 上传时间 03-09
  • 作者 网友投稿
  • 肖像权 人物画像及字体仅供参考
栏目分类 更多 >
热门推荐 更多 >
自适应 html5 微信图片 响应式 微信公众平台 微信模板 企业网站 微信素材 单页式简历模板 微信文章
您可能会喜欢的其他模板