<span id="year"></span>
<script>
realSysTime(year);
function realSysTime(showyear) {
var now = new Date(); //获取当前时间
var year = now.getFullYear(); //获取年份
var month = now.getMonth(); //获取月份
var date = now.getDate(); //获取日期
var hour = now.getHours(); //获取小时
var minute = now.getMinutes(); //获取分钟
var seconds = now.getSeconds(); //获取秒
//当不是两位数时在前面加0
if(date<10){
date='0'+date;
}
if(hour<10){
hour='0'+hour;
}
if(minute<10){
minute='0'+minute;
}
if(seconds<10){
seconds='0'+seconds;
}
month = month + 1;
if(month<10){
month='0'+month;
}
var newyear = year + "-" + month + "-" + date + " " +hour + ":" + minute + ":" + seconds;
showyear.innerHTML = newyear;
setInterval("realSysTime(year)", 1000); //1秒之后继续执行这个方法
}
</script>
效果:
小小前端,希望大家多提提意见,共同成长。