指引网

当前位置: 主页 > 网页制作 > WEB开发 >

js获取当前周、上一周、下一周日期

来源:网络 作者:佚名 点击: 时间:2017-07-18 23:46
[摘要]  本文主要介绍了js获取当前周、上一周、下一周日期的实例,具有很好的参考价值。下面跟着小编一起来看下吧

效果图:

代码如下:

 代码如下 复制代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

  <metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>

  <title>日历操作</title>

  <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>

  <buttonid="last-week">上一周</button><buttonid="next-week">下一周</button>

  <tableid="monitor">

    <tr>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

      <td></td>

    </tr>

  </table>

</body>

</html>


------分隔线----------------------------