js获取日期函数使用详解及示例演示,下面是一个页面实时时钟:
function startTime() {
var today = new Date()
var years = today.getFullYear();//年
var months = today.getMonth();//月份从0开始
var d = today.getDate();//日期
var h = today.getHours();//小时
var m = today.getMinutes();//分钟
var s = today.getSeconds();//秒
var w = today.getDay();//星期从0开始
// add a zero in front of numbers<10
months = months + 1
months = checkTime(months)
d = checkTime(d)
m = checkTime(m)
s = checkTime(s)
var weekday = new Array(7)
weekday[0] = “星期日”
weekday[1] = “星期一”
weekday[2] = “星期二”
weekday[3] = “星期三”
weekday[4] = “星期四”
weekday[5] = “星期五”
weekday[6] = “星期六”
var w = weekday[w]
document.getElementById(‘timeSpan’).innerHTML = years + “年” + months + “月”
+ d + “日 ” + w + ” ” + h + “:” + m + “:” + s;
t = setTimeout(‘startTime()’, 500)
}
function checkTime(i) {
if (i < 10) {
i = “0″ + i
}
return i
}
window.onload = startTime;