JS判断年月

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <style type="text/css">
   div{
    width: 300px;
    height: 300px;
    border: 1 solid aqua;
   
   }
  </style>
 </head>
 <body>
   <script type="text/javascript">
    var str  = prompt("请输入一个数");
    var strs  = str.split(" ");
    var y  = parseInt(strs[0]);
    var m = parseInt(strs[1]);
    var max =new  Date(y,m,0).getDate();
    var w = new Date(y +"/"+m +"/"+ "01").getDay();
    var html = '<div class="box">'+
         '<table>'+
          '<tr class ="title"><th colspan ="7">'+y +'年'+m+'月</th></tr>'+
          '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td>';
          '<tr>';
       for(var d=1;d<=max; ++d){
        if(w && d==1){
          html+='<td colspan = " ' +w+' "></td>';
        }
                html +='<td>'+d+'</td>';
      
         if (w==6 && d!=max){
        html += '<tr></tr>';
       }else if ( d==max ){
        html +='</tr>';
       }
       w =(w +1 >6) ? 0:w +1 ;
      }
       html += '</table>';
       html += '</div>';
       
       
       document.write(html);
   </script>
 </body>
</html>

posted @ 2020-04-04 16:18  Smile*^  阅读(588)  评论(0编辑  收藏  举报