js制作简单的时钟
getFullYear():获取四位数年份
getMonth():获取月份
getDate:获取日期
getDay:获取一周中的周几
getHours():获取小时
getMinutes():获取分钟
getSeconde():获取秒数
setInterval(callback, time):定时器方法
callback:回调函数
time:执行函数的间隔时间,单位毫秒
注意:代码要放在body标签下面执行。
<script>
setInterval(show_time, 1000);
show_time();
function show_time(){
var time = new Date();
var week = ["日", "一", "二", "三", "四", "五", "六"];
var str = time.getFullYear() + "年" + get_two(time.getMonth() + 1) + "月" + get_two(time.getDate()) + "日 周"
+ week[time.getDay()] + " " + get_two(time.getHours()) + ":" + get_two(time.getMinutes()) + ":" + get_two(time.getSeconds());
document.body.innerHTML = str;
}
//将小于10的数变为00,01,02...的样式
function get_two(num){
return num < 10 ? "0" + num : num;
}
</script
效果