服务器时间东八时区 js 时间 倒计时

[code]
var Damin_Time = function(enddate, tageIds, Func) {
var GetNewServiceTime = function() {
var serverTime = "";
jQuery.ajax({
type: "post",
url: "/timer.html?type=timer", /*获取服务器时间[现在到1970-1-1毫秒数]*/
async: false,
success: function(data) { serverTime = data; }
});
return serverTime;
}
var time = GetNewServiceTime();
var _timeOffset = ((-1 * (new Date()).getTimezoneOffset()) – (8 * 60)) * 60000; //时区
if(new Date(time-_timeOffset)>new Date(enddate)) return;
Func();//加载函数
var format = function(str, json) {
return str.replace(/{(\d)}/g, function(a, key) {
return json[key];
});
};
var p = function(s) {
return s < 10 ? ‘0’ + s : s; } var show = function(time, enddate) { var dateHtml = "{0}/{1}/{2} {3}:{4}:{5}"; var timeOffset = ((-1 * (new Date()).getTimezoneOffset()) – (8 * 60)) * 60000; //时区 var now = new Date(time – timeOffset); //得到当前东八区时间 //var _date = format(dateHtml, [p((now.getYear())), p((now.getMonth() + 1)), p(now.getDate()), p(now.getHours()), p(now.getMinutes()), p(now.getSeconds())]);//显示现在时间 var nMS = new Date(enddate).getTime() – now.getTime(); //计算两时间差值 if (nMS > 0) {
var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));
var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
var nM = Math.floor(nMS / (1000 * 60)) % 60;
var nS = Math.floor(nMS / 1000) % 60;
return nD + "^" + nH + "^" + nM + "^" + nS
}
return 0;
}

initTime = time;
var x = show(time, enddate);
timer = setInterval(function() {
time = parseInt(time) + parseInt(1000);
x = show(time, enddate);
if (x != 0 && tageIds.length == 4)
{ $("#" + tageIds[0]).html(x.split(‘^’)[0]); $("#" + tageIds[1]).html(x.split(‘^’)[1]); $("#" + tageIds[2]).html(x.split(‘^’)[2]); $("#" + tageIds[3]).html(x.split(‘^’)[3]); }
else { $("#" + tageIds[0]).html("0"); $("#" + tageIds[1]).html("0"); $("#" + tageIds[2]).html("0"); $("#" + tageIds[3]).html("0"); }
}, 1000);
}
[/code]

用法:
1 var endDate = “2012/04/15 15:00:00”;//结束时间
2 var Func = function() { $(“#nav”).after(”
<div class=”\&quot;w_ad\&quot;”><span id=”\&quot;spd\&quot;”>00</span>:<span id=”\&quot;sph\&quot;”>00</span>:<span id=”\&quot;spm\&quot;”>00</span>:<span id=”\&quot;sps\&quot;”>00</span></div>
“); }
3 Damin_Time(endDate, new Array(‘spd’, ‘sph’, ‘spm’, ‘sps’), Func);
里面用到了Jquery 注意加载
附加:一个C#获取现在到1970-1-1毫秒算法
1 string times = (DateTime.Now.Subtract(DateTime.Parse(“1970-1-1”)).TotalMilliseconds – (8 * 60 * 60 * 1000)).ToString(“F0”);

原文:http://www.cnblogs.com/baily/archive/2012/04/11/2442353.html