js获取当前时间并以时钟显示

<script type="text/javascript">
window.onload = function getTime(){
var st = showtime();
//对文本框赋值
document.getElementById("clock").innerText= st;
//设置过1000毫秒就是1秒,调用showtime方法
setTimeout("getTime()",1000);

}


function showtime()
{
today=new Date();
var year=today.getYear();
var month=today.getMonth()+1;
var day=today.getDate();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
// liehuo.net
var timeValue1 = ((hours < 10) ? "0" : "") + hours+"";
timeValue1 += ((minutes < 10) ? ":0" : ":") + minutes+"";
timeValue1 += ((seconds < 10) ? ":0" : ":") + seconds+"";
var timeValue2 = year;
timeValue2 += ((month < 10) ? "-0" : "-") + month+"";
timeValue2 += ((day < 10) ? "-0" : "-") + day+"";
var timetext=timeValue2+" "+timeValue1
return timetext;
}

</script>


<label id="clock"></label> 

你可能感兴趣的:(js获取当前时间并以时钟显示)