使用js获取时间
引入jq
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.js"></script>
html代码
<div id="date"></div>
编写js代码
$(function(){
setInterval(updateTime, 1000); //每秒刷新
updateTime();
})
function updateTime() {
var date = new Date();
this.year = date.getFullYear(); //年
this.month = date.getMonth() + 1; //月
this.date = date.getDate(); //日
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()]; //星期
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //时
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //分
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //秒
var currentTime = this.year + "年" + this.month + "月" + this.date + "日" + this.day + ","+this.hour + ":" + this.minute + ":" + this.second ;
$("#date").html(currentTime);
}