javascript显示动态时间的方法汇总
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
1.第一种方法
<table> <tr> <td>当前时间:</td> <td id="CurrentTime"></td> <script type="text/javascript"> function changetime(){ var ary = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; var Timehtml = document.getElementById('CurrentTime'); var date = new Date(); Timehtml.innerHTML = ''+date.toLocaleString()+' '+ary[date.getDay()]; } </script> </tr> </table>
2.第二种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="gb2312"> <title></title> <script type="text/javascript"> window.onload=function(){ setInterval(function(){ var date=new Date(); var year=date.getFullYear(); //获取当前年份 var mon=date.getMonth()+1; //获取当前月份 var da=date.getDate(); //获取当前日 var day=date.getDay(); //获取当前星期几 var h=date.getHours(); //获取小时 var m=date.getMinutes(); //获取分钟 var s=date.getSeconds(); //获取秒 var d=document.getElementById('Date'); d.innerHTML='当前时间:'+year+'年'+mon+'月'+da+'日'+'星期'+day+' '+h+':'+m+':'+s; },1000) } </script> </head> <body> <div id="Date"> </div> </body> </html>
3.第一种方法
<!DOCTYPE html> <html> <body> <div id="show"></div> <script type="text/javascript"> window.onload = function() { var show = document.getElementById("show"); setInterval(function() { var time = new Date(); // 程序计时的月从0开始取值后+1 var m = time.getMonth() + 1; var t = time.getFullYear() + "-" + m + "-" time.getDate() + " " + time.getHours() + ":" time.getMinutes() + ":" + time.getSeconds(); show.innerHTML = t; }, 1000); }; </script> </body> </html>
下一篇:Django+Vue跨域环境配置详解