JavaScript实时变化的时间
分类:实例代码
JavaScript实时变化的时间属于前端实例代码,有关更多实例代码大家可以查看。
在很多网站都有这样的功能,能够显示当前的事件,这样或许能够增加美观度,当然更加能够提高网页的人性化程度,能够让给用户实时了解当前时间,这样能够给网站增色不少。下面分享一段能够获取当前时间,并且能够进行格式化的代码:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.pipipi.net/" /> <title>javascript实现的时间显示代码-前端教程网</title> <script type="text/javascript"> function clockon(bgclock){ var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var date = now.getDate(); var day = now.getDay(); var hour = now.getHours(); var minu = now.getMinutes(); var sec = now.getSeconds(); var week; month = month+1; if(month<10)month="0"+month; if(date<10)date="0"+date; if(hour<10)hour="0"+hour; if(minu<10)minu="0"+minu; if(sec<10)sec="0"+sec; var arr_week = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); week = arr_week[day]; var time = ""; time = year+"年"+month+"月"+date+"日"+week+""+hour+":"+minu+":"+sec; bgclock.innerHTML="["+time+"]"; var timer = setTimeout("clockon(bgclock)",200); } window.onload=function(){ var bgclock=document.getElementById("bgclock"); clockon(bgclock); } </script> </head> <body> <div id="bgclock"></div> </body> </body> </html>
JavaScript实时变化的时间,这样的场景在实际项目中还是用的比较多的,关于JavaScript实时变化的时间就介绍到这了。