问题描述:
一般网站上都有年月日时分秒,一秒一秒的走。还有星期。
解决办法:
html:
<div id="time"></div>
script:
第一种办法:
<script type="text/javascript">
window.onload=function (){
setInterval("document.getElementById('time').innerHTML=new Date().toLocaleString()+''+''.charAt(new Date().getDay());",1000);
}
</script>
第二种办法:
<script>
$(function() {
gettime();
setInterval(gettime,1000);
});
var gettime=function(){
var myDate = new Date;
var year = myDate.getFullYear(); //获取当前年
var mon = myDate.getMonth() + 1; //获取当前月
var date = myDate.getDate(); //获取当前日
var h = myDate.getHours()< 10 ? "0" + myDate.getHours() : myDate.getHours();//获取当前小时数(0-23)
var m = myDate.getMinutes()< 10 ? "0" + myDate.getMinutes() : myDate.getMinutes();//获取当前分钟数(0-59)
var s = myDate.getSeconds()< 10 ? "0" + myDate.getSeconds() : myDate.getSeconds();//获取当前秒
var week = myDate.getDay();
var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
$("#time").html("<span class='time'>"+h + ":" + m + ":" + s +'</span> <span class="vertical-line"> |</span> <span class="date">'+year + "-" + mon + "-" + date + " " + weeks[week]+"</span>");
}
</script>