日期的获取

获取本周的起止日期

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        window.onload = function(){
         
            var a = document.getElementById('a');
            var b = document.getElementById('b');
            //var clen = cells.length;
            var currentFirstDate;
            var formatDate = function(date){
             
                var year = date.getFullYear()+'-';
                var month = (date.getMonth()+1);
                var day = date.getDate();
                 if(month<10)
                 {
                    month='0'+month+'-';
                 }else{
                    month=month+'-'
                 }

                if(day<10)
                 {
                    day='0'+day;
                 }

                return year+month+day+' ';
            };
            var addDate= function(date,n){       
                date.setDate(date.getDate()+n);        
                return date;
            };
            var setDate = function(date){
             
                var week = date.getDay()-1;
                date = addDate(date,week*-1);
                currentFirstDate = new Date(date);
                a.value=formatDate( addDate(date,0));
                b.value=formatDate( addDate(date,6));       
            };
             
            setDate(new Date());
        }
    </script>
</head>
<body>
<input type="text" id="a" value="">
<input type="text" id="b" value="">
</body>
</html>
demo1

获取上一周下一周的日期

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
     
    td{
     
        border:1px solid;
    }
     
    button{
     
        margin: 5px;
    }
         
    </style>
    <script>
        window.onload = function(){
         
            var cells = document.getElementById('monitor').getElementsByTagName('td');
            var clen = cells.length;
            var currentFirstDate;
            var formatDate = function(date){
             
                var year = date.getFullYear()+'';
                var month = (date.getMonth()+1)+'';
                var day = date.getDate()+'';
                var week = '('+['星期天','星期一','星期二','星期三','星期四','星期五','星期六'][date.getDay()]+')';
                 
                return year+month+day+' '+week;
            };
            var addDate= function(date,n){       
                date.setDate(date.getDate()+n);        
                return date;
            };
            var setDate = function(date){
             
                var week = date.getDay()-1;
                date = addDate(date,week*-1);
                currentFirstDate = new Date(date);
                 
                for(var i = 0;i<clen;i++){
                 
                    cells[i].innerHTML = formatDate(i==0 ? date : addDate(date,1));
                }
                 
            };
             
            document.getElementById('last-week').onclick = function(){
                 
                setDate(addDate(currentFirstDate,-7));         
            };
             
            document.getElementById('next-week').onclick = function(){
                 
                setDate(addDate(currentFirstDate,7));
            };
             
            setDate(new Date());
        }
    </script>
</head>
<body>
    <button id="last-week">上一周</button><button id="next-week">下一周</button>
    <table id="monitor">
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
</body>
</html>
demo2

 

posted @ 2016-07-16 16:53  Secretmm  阅读(127)  评论(0编辑  收藏  举报