您的位置:首页 > Web前端 > JavaScript

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

2012-11-01 17:18 429 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: