关于倒计时的时区问题

1. 用Ajax获取服务器上的时间,不会被缓存

1 var now_date = new Date($.ajax({async: false}).getResponseHeader("Date"));
2 var server_time = now_date.getTime();
3 /*{$smarty.now}smarty获取服务器上UTC的秒数,但是服务器会缓存,不能用
4 var now = new Date(parseInt($("#now").val()*1000));//秒数变成毫秒
5 var nowTime = now.getTime();*/

2. 把end time对应时区的时间转换成秒数

1 var d = new Date(Date.UTC(2014,7,24,14,0,0));//毫秒
2 var time = d.getTime();

3. 调用time.js去计算(重点片段代码)

 1 TimerManager.prototype.start = function(dateToCountTo,server_time) { 
 2     this.totalDuration = dateToCountTo - server_time;
 3     this.startTime = server_time;
 4     return this.intervalId = setInterval(this.run, 1000);
 5 };
 6 TimerManager.prototype.run = function() {
 7     var elapsed, hrs, mins, remaining, secs;
 8     count = count + 1000;
 9     //elapsed = new Date().getTime() - this.startTime;
10     remaining = this.totalDuration - count;

4. 注意有的时区会有夏令时(把时钟调快了1个小时,需要自己计算一下到达时间)

 1 Date.prototype.stdTimezoneOffset = function() {
 2         var jan = new Date(this.getFullYear(), 0, 1);
 3         var jul = new Date(this.getFullYear(), 6, 1);
 4         return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
 5     }
 6     Date.prototype.dst = function() {
 7         return this.getTimezoneOffset() < this.stdTimezoneOffset();
 8     }
 9     var today = new Date();
10     if (today.dst()) { 
11         //console.info ("Daylight savings time!");
12     }

 

posted @ 2014-06-08 13:33  connie1120  阅读(449)  评论(0编辑  收藏  举报