JavaScript实时变化的时间

2018-2-12 14:44| 作者: admin| 查看: 520| 评论: 0|来自: 蚂蚁部落

在很多网站都有这样的功能,能够显示当前的事件,这样或许能够增加美观度,当然更加能够提高网页的人性化程度,能够让给用户实时了解当前时间,这样能够给网站增色不少。下面分享一段能够获取当前时间,并且能够进行格式化的代码:

[HTML] 纯文本查看 复制代码运行代码
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.softwhy.com/" />
<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>

鲜花

握手

雷人

路过

鸡蛋

最新评论

返回顶部