美文网首页
jQuery 获取两个时间的时间差

jQuery 获取两个时间的时间差

作者: 白衣诗人 | 来源:发表于2018-12-26 18:02 被阅读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)
        });
    

    相关文章

      网友评论

          本文标题:jQuery 获取两个时间的时间差

          本文链接:https://www.haomeiwen.com/subject/izhklqtx.html