Skip to content

Instantly share code, notes, and snippets.

@SFantasy
Created May 25, 2013 17:52
Show Gist options
  • Save SFantasy/5650053 to your computer and use it in GitHub Desktop.
Save SFantasy/5650053 to your computer and use it in GitHub Desktop.
JavaScript实时获取时间
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Clock</title>
<script>
var i;
function getTime () {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDay();
var hour = date.getHours();
var minute = date.getMinutes();
var seconds = date.getSeconds();
var time = "";
time += year + "年-";
time += month + "月-";
time += day + "日 "
time += hour + "点 ";
time += minute + "分 ";
time += seconds + "秒";
document.getElementById('showtime').value = time;
i = setTimeout('getTime()', 1);
}
</script>
</head>
<body onload="getTime()">
<input id="showtime" type="text" size="100">
<button onclick="getTime()">Get Time!</button>
<button onclick="clearTimeout(i)">Stop!</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment