js动态显示当前时间+数字大小转换+小于9前面补0
<script type="text/javascript">
function getTime(){
var myDate = new Date();
// 年份
document.getElementById("year").innerHTML = myDate.getFullYear() + "年";
// 星期
var w = myDate.getDay();
var week = document.getElementById("week");
if(w == 0) {
week.innerHTML = "星期日";
} else if(w == 1) {
week.innerHTML = "星期一";
} else if(w == 2) {
week.innerHTML = "星期二";
} else if(w == 3) {
week.innerHTML = "星期三";
} else if(week == 4) {
week.innerHTML = "星期四";
} else if(w == 5) {
week.innerHTML = "星期五";
} else if(w == 6) {
week.innerHTML = "星期六";
}
// 月份
var y;
var yue = myDate.getMonth() + 1;
if(yue < 9) {
y = "0" + yue;
} else {
y = yue;
}
// 日
var d;
var day = myDate.getDate();
if(day < 10) {
d = "0" + day;
} else {
d = day;
}
var month = y + "月" + d;
document.getElementById("month").innerHTML = month;
// 小时
var h;
var hour = myDate.getHours();
if(hour < 10) {
h = "0" + hour;
} else {
h = hour;
}
document.getElementById("hour").innerHTML = h + ":";
// 分钟
var m;
var minutes = myDate.getMinutes();
if(minutes < 10) {
m = "0" + minutes;
} else {
m = minutes;
}
document.getElementById("minutes").innerHTML = m;
}
window.setInterval("getTime()",1000);
</script>