写在开头
时间戳在web端十分常用,通过JavaScript编写一个简单的时间戳,也就成为了一个js开发者的基础技能。直接上代码:
具体代码
<html>
<head>
<body>
<div id="now"></div>
</body>
<script type="text/javascript">
// 自动计时时间戳
function Time(){
var day = new Date;
var d = day.getDate();
var mon = day.getMonth()+1;
var min = day.getMinutes();
var sec = day.getSeconds();
var h = day.getHours();
switch (d){
case 0: week_str="星期日";break;
case 1: week_str="星期一";break;
case 2: week_str="星期二";break;
case 3: week_str="星期三";break;
case 4: week_str="星期四";break;
case 5: week_str="星期五";break;
default: week_str="星期六";break;
}
var str = time(mon) + "月" + time(d) + "日" + week_str + time(h) + ":" + time(min) + ":" + time(sec);
//数字补位
function time(num){
if(parseInt(num) < 10){
num = '0'+num;
}
return num;
}
var now = document.getElementById('now');
now.innerHTML = str;
setTimeout(Time,1000)
};
window.onloda = function(){Time()};
</script>
</head>
</html>
结尾
代码一般,只为达到展示目的,还望大佬指出不足!