无论GMT是访客,都可以在此倒计时脚本中使用GMT + 4时间

时间:2012-05-14 19:02:43

标签: javascript countdown

这个倒计时时间脚本工作正常,但对于所有GMT访客都不同。我想全球时间GMT + 4适合所有人,无论访问者来自哪个国家,时间都应该是全球性的

我需要添加什么,以便我的脚本只读取一般时间和我需要放置的位置?

如果有人可以编辑我的脚本并在

中包含此功能,我将不胜感激

提前谢谢

    <script type="text/javascript">
var events = new Array();

events[1]={name: 'Blood Castle:', startUp: new Array(
    {hour:0,minute:25},
    {hour:2,minute:25},
    {hour:4,minute:25},
    {hour:6,minute:25},
    {hour:8,minute:25},
    {hour:10,minute:25},
    {hour:12,minute:25},
    {hour:14,minute:25},
    {hour:16,minute:25},
    {hour:18,minute:25},
    {hour:20,minute:25},
    {hour:22,minute:25}
)}

events[2]={name: 'Devil Square:', startUp: new Array(
    {hour:1,minute:55},
    {hour:3,minute:55},
    {hour:5,minute:55},
    {hour:7,minute:55},
    {hour:9,minute:55},
    {hour:11,minute:55},
    {hour:13,minute:55},
    {hour:15,minute:55},
    {hour:17,minute:55},
    {hour:19,minute:55},
    {hour:21,minute:55},
    {hour:23,minute:55}
)}

events[3]={name: 'Chaos Castle:', startUp: new Array(
    {hour:0,minute:55},
    {hour:2,minute:55},
    {hour:4,minute:55},
    {hour:6,minute:55},
    {hour:8,minute:55},
    {hour:10,minute:55},
    {hour:12,minute:55},
    {hour:14,minute:55},
    {hour:16,minute:55},
    {hour:18,minute:55},
    {hour:20,minute:55},
    {hour:22,minute:55}
)}

events[4]={name: 'Red Dragon:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[5]={name: 'Gold Invasion:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[6]={name: 'White Wizard:', startUp: new Array(
    {hour:0,minute:0},
    {hour:2,minute:0},
    {hour:4,minute:0},
    {hour:6,minute:0},
    {hour:8,minute:0},
    {hour:10,minute:0},
    {hour:12,minute:0},
    {hour:14,minute:0},
    {hour:16,minute:0},
    {hour:18,minute:0},
    {hour:20,minute:0},
    {hour:22,minute:0}
)}

events[7]={name: 'Blue:', startUp: new Array(
    {hour:0,minute:40},
    {hour:1,minute:40},
    {hour:2,minute:40},
    {hour:3,minute:40},
    {hour:4,minute:40},
    {hour:5,minute:40},
    {hour:6,minute:40},
    {hour:7,minute:40},
    {hour:8,minute:40},
    {hour:9,minute:40},
    {hour:10,minute:40},
    {hour:11,minute:40},
    {hour:12,minute:40},
    {hour:13,minute:40},
    {hour:14,minute:40},
    {hour:15,minute:40},
    {hour:16,minute:40},
    {hour:17,minute:40},
    {hour:18,minute:40},
    {hour:19,minute:40},
    {hour:20,minute:40},
    {hour:21,minute:40},
    {hour:22,minute:40},
    {hour:23,minute:40},
    {hour:24,minute:40}
)}

events[7]={name: 'Hide&Seek:', startUp: new Array(
    {hour:0,minute:42},
    {hour:1,minute:22},
    {hour:2,minute:32},
    {hour:3,minute:42},
    {hour:4,minute:52},
    {hour:6,minute:02},
    {hour:7,minute:12},
    {hour:8,minute:22},
    {hour:9,minute:32},
    {hour:10,minute:42},
    {hour:11,minute:52},
    {hour:13,minute:02},
    {hour:14,minute:12},
    {hour:15,minute:22},
    {hour:16,minute:32},
    {hour:17,minute:42},
    {hour:18,minute:52},
    {hour:20,minute:02},
    {hour:21,minute:12},
    {hour:22,minute:22},
    {hour:23,minute:32}
)}

events[8]={name: 'Sky:', startUp: new Array(
    {hour:1,minute:5},
    {hour:4,minute:5},
    {hour:7,minute:5},
    {hour:10,minute:5},
    {hour:13,minute:5},
    {hour:16,minute:5},
    {hour:19,minute:5},
    {hour:23,minute:5}
)}

events[9]={name: 'Boss Attack:', startUp: new Array(
    {hour:1,minute:50},
    {hour:4,minute:50},
    {hour:7,minute:50},
    {hour:10,minute:50},
    {hour:13,minute:50},
    {hour:16,minute:50},
    {hour:23,minute:50}
)}


events[10]={name: 'Happy Hour:', startUp: new Array(
    {hour:5,minute:5},
    {hour:2,minute:5},
    {hour:8,minute:5},
    {hour:11,minute:5},
    {hour:14,minute:5},
    {hour:17,minute:5},
    {hour:20,minute:5},
    {hour:0,minute:5}
)}

events[11]={name: 'Hit and Up:', startUp: new Array(
    {hour:0,minute:20},
    {hour:2,minute:20},
    {hour:5,minute:20},
    {hour:8,minute:20},
    {hour:11,minute:20},
    {hour:14,minute:20},
    {hour:20,minute:20}
)}

events[12]={name: 'Raklion:', startUp: new Array(
    {hour:0,minute:15},
    {hour:3,minute:15},
    {hour:6,minute:15},
    {hour:9,minute:15},
    {hour:12,minute:15},
    {hour:15,minute:15},
    {hour:18,minute:15},
    {hour:21,minute:15}
)}

events[13]={name: 'Moss:', startUp: new Array(
    {hour:3,minute:35},
    {hour:7,minute:35},
    {hour:11,minute:35},
    {hour:15,minute:35},
    {hour:19,minute:35},
    {hour:23,minute:35}
)}

events[14]={name: 'Illusion Temple:', startUp: new Array(
    {hour:0,minute:25},
    {hour:1,minute:25},
    {hour:2,minute:25},
    {hour:3,minute:25},
    {hour:4,minute:25},
    {hour:5,minute:25},
    {hour:6,minute:25},
    {hour:7,minute:25},
    {hour:8,minute:25},
    {hour:9,minute:25},
    {hour:10,minute:25},
    {hour:11,minute:25},
    {hour:12,minute:25},
    {hour:13,minute:25},
    {hour:14,minute:25},
    {hour:15,minute:25},
    {hour:16,minute:25},
    {hour:17,minute:25},
    {hour:18,minute:25},
    {hour:19,minute:25},
    {hour:20,minute:25},
    {hour:21,minute:25},
    {hour:22,minute:25},
    {hour:23,minute:25},
    {hour:24,minute:25}
)}

events[15]={name: 'Castle Deep:', startUp: new Array(
    {hour:1,minute:25},
    {hour:7,minute:25},
    {hour:13,minute:25},
    {hour:19,minute:25}
)}

events[16]={name: 'CryWolf:', startUp: new Array(
    {hour:1,minute:45},
    {hour:4,minute:45},
    {hour:7,minute:45},
    {hour:10,minute:45},
    {hour:13,minute:45},
    {hour:16,minute:20},
    {hour:19,minute:45},
    {hour:22,minute:45}
)}

var curTime=1336998502;
var dateTime=1336953600;
if(localStorage.curTime) {
    curTime = Number(localStorage.curTime);
} else {
    // use default set above
}
// not sure if we need to persist dateTime but doing it anyway!
if(localStorage.dateTime) {
    dateTime = Number(localStorage.dateTime);
} else {
    // use default set above
}
function timeLeft(i){
    for(j in events[i].startUp){
        tmp=events[i].startUp[j].hour*3600+events[i].startUp[j].minute*60
        if(dateTime+tmp>curTime){
            return dateTime+tmp-curTime;
        }
    }
    tmp=events[i].startUp[0].hour*3600+events[i].startUp[0].minute*60
    return dateTime+86400+tmp-curTime;
}

function getFormatedLeftTime($seconds){
    $second = $seconds % 60;
    $minutes = parseInt(($seconds / 60) % 60);
    $hour =  parseInt(($seconds / 3600) % 24);
    $days = parseInt($seconds / (24 * 3600));

    $ret = '';
    if ($days > 0)
        if ($days == 1) $ret += '1 day ';
        else $ret += $days + ' days ';


    if ($hour > 0){
        if ($hour < 10) $hour = '0' + $hour;
        $ret += $hour + ':';
    }else if ($days > 0) $ret += '00:';

    if($minutes < 10) $minutes = '0' + $minutes;

    $ret += $minutes + ':';

    if ($second < 10) $second = '0' + $second;

    $ret += $second;
    return $ret;

}

function updateTimes(){
    curTime++;
    localStorage.curTime = curTime; // save current time
    for (i in events){
        document.getElementById("timeLeft"+i).innerHTML=getFormatedLeftTime(timeLeft(i));
    }
}

for(i in events)
    document.getElementById("eventList").innerHTML+="<div style='float:right; color: #FFFF00;' id='timeLeft"+i+"'>"+getFormatedLeftTime(timeLeft(i))+"</div><div style=\"color: #00FFFF;\"><strong>"+events[i].name+"</strong></div>";
setInterval("updateTimes()", 1000);



</script>

2 个答案:

答案 0 :(得分:1)

如果您只毫秒 - 自 - 纪元格式处理时间,则无需担心时区。

因此,在服务器方面,计算正确的完成时间(以毫秒为单位),然后在下载脚本时将其发送给客户端。

在客户端,您只需要在服务器提供的时间和客户端的纪元之间计算出差异,并将不同的转换为小时,分钟,秒等。

答案 1 :(得分:0)

JavaScript日期有一个名为getTimezoneOffset()的方法,它以分钟为单位返回GMT的偏移量。您可以使用此值来协调“中心”时区:

var d = new Date();
d.toString(); // Mon May 14 2012 13:20:24 GMT-0600 (MDT)
d.getTimezoneOffset(); // => 360 (GMT-600)
var localTimeOffsetHours = d.getTimezoneOffset() / 60; // => 6