js获取当前周、上一周、下一周日期
程序员文章站
2022-06-13 18:24:26
效果图:
代码如下:
效果图:
代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html > <head> <meta http-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> <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>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!