码迷,mamicode.com
首页 > 其他好文 > 详细

日期的获取

时间:2016-07-16 21:07:19      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

获取本周的起止日期

技术分享
<!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

 

日期的获取

标签:

原文地址:http://www.cnblogs.com/Secretmm/p/5676832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!