Home  >  Article  >  Web Front-end  >  How to use js to get each time point based on the start time and end time

How to use js to get each time point based on the start time and end time

一个新手
一个新手Original
2017-09-09 10:04:392081browse


The effect of this blog (demo address included):
How to use js to get each time point based on the start time and end time

test.html file code

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/jquery-2.1.1.js"></script></head><body><p class="getDay"><a href="#">获取2017-08-28到2017-09-08该段时间内的所有以天为单位的日期</a></p><p class="getWeek"><a href="#">获取2017-06-05到2017-09-04该段时间内的所有以周一为单位的日期</a></p><p class="month"><a href="#">获取2016-06-01到2017-09-01该段时间内的所有以月为单位的日期</a></p><p class="year"><a href="#">获取2003-01-01到2017-01-01该段时间内的所有以年为单位的日期</a></p></body><script>

    $(document).ready(function () {
        $(".getDay").click(function () {
            alert(getDayAll("2017-08-28","2017-09-08"));
        })
        $(".getWeek").click(function () {
            alert(getWeekAll("2017-06-05","2017-09-04"));
        })
        $(".month").click(function () {
            alert(getMonthAll("2016-06-01","2017-09-01"));
        })
        $(".year").click(function () {
            alert(getYearAll("2003-01-0","2017-01-01"));
        })
    })    Date.prototype.format=function (){
        var s=&#39;&#39;;
        s+=this.getFullYear()+&#39;-&#39;;          // 获取年份。
        s+=(this.getMonth()+1)+"-";         // 获取月份。
        s+= this.getDate();                 // 获取日。
        return(s);                          // 返回日期。
    };    //按日查询
    function getDayAll(begin,end){
        var dateAllArr = new Array();        
        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;){
            dateAllArr.push((new Date(parseInt(k))).format().toString());
            k=k+24*60*60*1000;
        }        return dateAllArr;
    }    //按周查询
    function getWeekAll(begin,end){
        var dateAllArr = new Array();        
        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;){
            dateAllArr.push((new Date(parseInt(k))).format().toString());
            k=k+7*24*60*60*1000;
        }        return dateAllArr;
    }    function getMonthAll(begin,end) {
        var d1 = begin;        
        var d2 = end;        
        var dateArry = new Array();        
        var s1 = d1.split("-");        
        var s2 = d2.split("-");        
        var mCount = 0;        
        if (parseInt(s1[0]) < parseInt(s2[0])) {
            mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1])+1;
        } else {
            mCount = parseInt(s2[1]) - parseInt(s1[1])+1;
        }        if (mCount > 0) {            
        var startM = parseInt(s1[1]);            
        var startY = parseInt(s1[0]);            
        for (var i = 0; i < mCount; i++) {                
        if (startM < 12) {
                    dateArry[i] = startY + "-" + (startM>9 ? startM : "0" + startM);
                    startM += 1;
                } else {
                    dateArry[i] = startY + "-" + (startM > 9 ? startM : "0" + startM);
                    startM = 1;
                    startY += 1;
                }
            }
        }        
        return dateArry;
    }    
    function getYearAll(begin,end) {
        var d1 = begin;        
        var d2 = end;        
        var dateArry = new Array();        
        var s1 = d1.split("-");        
        var s2 = d2.split("-");        
        var mYearCount = parseInt(s2[0]) - parseInt(s1[0])+1;        
        var startY = parseInt(s1[0]);        
        for (var i = 0; i < mYearCount;i++) {
            dateArry[i] = startY;
            startY += 1;
        }        
        return dateArry;
    }
    </script>
 </html>

The above is the detailed content of How to use js to get each time point based on the start time and end time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn