jQuery 获取两个时间的时间差

2018-12-26  本文已影响0人  白衣诗人
<input type="text" placeholder="起始时间" id="up">
<input type="text" placeholder="结束时间" id="noUp">
<button type="button" id="btn">开始</button>
<ul class="dataUl">

</ul>
   li{
        display: inline-block;
        float: left;
    }
 Date.prototype.format = function() {
        var s = '';
        var mouth = (this.getMonth() + 1)>=10?(this.getMonth() + 1):('0'+(this.getMonth() + 1));
        var day = this.getDate()>=10?this.getDate():('0'+this.getDate());
        s += this.getFullYear() + '-'; // 获取年份。
        s += mouth + "-"; // 获取月份。
        s += day; // 获取日。
        return (s); // 返回日期。
    };
    function getAll(begin, end) {
        var ab = begin.split("-");
        var ae = end.split("-");
        var db = new Date();
        db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
        var de = new Date();
        de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
        var unixDb = db.getTime();
        var unixDe = de.getTime();
        for (var k = unixDb; k <= unixDe;) {
            $(".dataUl").append("<li>"+(new Date(parseInt(k))).format()+"</li>");
            k = k + 24 * 60 * 60 * 1000;
        }
    }
    $("#btn").on('click',function () {
        var up =$("#up").val();
        var noUp = $("#noUp").val();
        getAll(up,noUp)
    });
上一篇下一篇

猜你喜欢

热点阅读