在html页面上用一个div显示时间
点击按钮1在当前时间上加一个月在div里面显示
点击按钮2在当时时间上加一年在dic里面显示
点击按钮3在当前时间上加24个小时在div里面显示

Javascript加时间问题日期
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 沐卿゚ 2022-02-22 14:28关注
看看这个
```html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <div id="box">222</div> <button onclick="data(1)">显示年</button> <button onclick="data(2)">显示月</button> <button onclick="data(3)">显示时间</button> </body> </html> <script> function data(a){ var d=new Date(); //设置一个字符串表示的日期对象 if(a==1){ yy=d.getFullYear(); //取满年 document.getElementById('box').innerHTML=yy+'年' }else if(a==2){ mm=d.getMonth()+1; //取月份 document.getElementById('box').innerHTML=mm+'月' }else if(a==3){ intHours = d.getHours(); intMinutes = d.getMinutes(); intSeconds = d.getSeconds(); document.getElementById('box').innerHTML=intHours+':'+intMinutes+':'+intSeconds } } </script>
```
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报