当前系统时间<em style="color:red" id="Stime">2012-12-05 15:00:00</em>
1.首先,用Ajax请求后台,获取系统时间(这里忽略不计请求时间)
function systemTime() {
jQuery.ajax({
type: "POST",
url: "/School/LiveBroadCast/GetSystemTime.aspx",
cache: false,
async: true,
contentType: "application/json",
data: {},
success: function (data) {
if (data != "") {
$("#Stime").text(data);
setInterval("TimeTick()", 1000);
}
}
});
}
2.设置定时器,定时调用时间加载方法(这里以秒为单位)
function TimeTick() {
var timeNow = $("#Stime").text();
timeNow = new Date(Date.parse(timeNow.replace(/-/g, "/")) + 1000)
var now = timeNow.getFullYear() + "-" + Appendzero((timeNow.getMonth() + 1)) + "-" + Appendzero(timeNow.getDate()) + " " + Appendzero(timeNow.getHours()) + ":" + Appendzero(timeNow.getMinutes()) + ":" + Appendzero(timeNow.getSeconds());
$("#Stime").text(now);
}
//给不到10的日期补零
function Appendzero (obj) {
if (obj < 10) return "0" + obj; else return obj;
}