首页 > 代码库 > js获取实时日期和时间
js获取实时日期和时间
//在相应位置显示时间日期
<div style=" width:400px; height:50px">
<span id="showtime"></span> <!--走马灯文字 <marquee direction="left">需要滚动的内容</marquee>-->
</div>
</body>
js获取时间日期
<script type="text/javascript">
function showTime(){
var week=new Array(‘星期一‘,‘星期二‘,‘星期三‘,‘星期四‘,‘星期五‘,‘星期六‘,‘星期日‘);
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var date = now.getDate();
var day = now.getDay();
var hour = now.getHours();
var minutes = now.getMinutes();
var second = now.getSeconds();
month<10?month=‘0‘+month:month;
month=month+1;
hour<10?hour=‘0‘+hour:hour;
minutes<10?minutes=‘0‘+minutes:minutes;
second<10?second=‘0‘+second:second;
var now_time = ‘当前时间:‘ + year + ‘年‘ + month + ‘月‘ + date + ‘日‘ + ‘ ‘ + hour + ‘:‘ + minutes + ‘:‘ + ‘‘ + second + ‘ ‘ + week[day - 1];
document.getElementById(‘showtime‘).innerHTML=now_time;
setTimeout("showTime();",1000); //每隔1秒刷新一次
}
</script>
js获取实时日期和时间