2012-03-08 19 views
1

我想根據小時(GMT)製作一個顯示不同文本行的網站。我曾嘗試使用JavaScript,但我非常初學者!我已經設法將下面的代碼拼湊在一起,但似乎無法使其工作。任何幫助感謝!每小時不同的文本行數

function getTime(zone, success) { 
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone, 
     ud = 'json' + (+new Date()); 
    window[ud]= function(o){ 
     success && success(new Date(o.datetime)); 
    }; 
    document.getElementsByTagName('head')[0].appendChild((function(){ 
     var s = document.createElement('script'); 
     s.type = 'text/javascript'; 
     s.src = url + '&callback=' + ud; 
     return s; 
    })()); 
} 

getTime('GMT', function(time){ 

    if (time>10 && time<11) 
    { 
    document.write("<b>Paris</b>"); 
    } 
    ; 
}); 

+0

傻冒missusing'document.write'。現在它清除您網頁中的所有內容。您需要使用一些DOM操作。 – Teemu 2012-03-08 12:11:34

+0

由於文本不是基於用戶的當地時間,所以不應該在服務器上完成? – stark 2012-03-08 12:12:03

回答

0

如何:

var now = new Date(); 
var utcMillis = now.getTime() + now.getTimzoneOffset() * 60000; 
var hour = new Date(utcMillis).getHours(); // 0-23 

switch(hour) { 
    case 10: document.write("<b>Paris</b>"); break; 
    //... 
} 

一些很好的信息在這裏:http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329

你也可以把你的短語在數組(這可能會從服務器獲取):

var phrases = ["Hi!", "blah", "more blah", ...]; 

...然後你可以替換上面的開關:

document.write("<b>" + phrases[hour] + "</b>"); 

你可能想保存自己一些時間和使用一個框架,使東西跨瀏覽器幾乎相同的方式。 JQuery是我的最愛,但有很多。這種庫可以很容易地操縱在你的頁面內容,從服務器獲取JSON,等等等等

0

乍一看:

  1. o.datetime是錯誤的格式化JS日期的方法。
  2. 返回的new Date()一個實例來獲取小時使用.getHours()

你可以看看這裏找到一種方法,你dateString從o.datetimeHow can I convert string to datetime with format specification in JavaScript?

使用time.getHours() > 12而不是time > 12

如果轉換您使用:getDateFromFormat() from http://www.mattkruse.com/javascript/date/我認爲轉換將類似E, dd MMM hh:mm:ss +0000


簡單回答:

你可以只解析hour到你的回調:(這裏http://json-time.appspot.com/time.json?tz=GMT請參閱JSON)

function getTime(zone, success) { 
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone, 
     ud = 'json' + (+new Date()); 
    window[ud]= function(o){ 
     success && success(o.hour); // See o.hour here 
    }; 
    document.getElementsByTagName('head')[0].appendChild((function(){ 
     var s = document.createElement('script'); 
     s.type = 'text/javascript'; 
     s.src = url + '&callback=' + ud; 
     return s; 
    })()); 
} 

getTime('GMT', function(time){ 
    if (time>10 && time<11) { 
     document.write("<b>Paris</b>"); 
    } 
}); 
1

JavaScript有一個Date類。

var hour = new Date().getHours(); 

if(...) { 
    // do something 
} 

此代碼提取

if (hour>10 && hour<11) 

不能與小時的工作,因爲時間不能> 10和11 <在同一時間(小時是int)。

0

我已經創建了一個快速jsfiddle頁面:http://jsfiddle.net/eBAGS/5/帶有應該做你需要的演示。它從用戶PC而不是從服務器獲取時間,但可以修改。

HTML

<button onClick="getPhrase()">Show me a phrase for this hour</button> 
<div id="placeHolder"></div> 

的Javascript

function getPhrase() { 
    h = new Date().getHours(); //Get the current hour 

    phrase = new Array(); //Create an array of phrases 
    phrase[1] = 'Hello'; 
    phrase[2] = 'there'; 
    phrase[3] = 'this'; 
    phrase[4] = 'will'; 
    phrase[5] = 'show'; 
    phrase[6] = 'a'; 
    phrase[7] = 'different'; 
    phrase[8] = 'message'; 
    phrase[9] = 'depending'; 
    phrase[10] = 'on'; 
    phrase[11] = 'the'; 
    phrase[12] = 'hour'; 
    phrase[13] = 'of'; 
    phrase[14] = 'day'; 
    phrase[15] = 'that'; 
    phrase[16] = 'you'; 
    phrase[17] = 'look'; 
    phrase[18] = 'at'; 
    phrase[19] = 'it'; 
    phrase[20] = '!'; 
    phrase[21] = 'W'; 
    phrase[22] = 'X'; 
    phrase[23] = 'Y'; 
    phrase[24] = 'Z'; 

    document.getElementById('placeHolder').innerHTML = phrase[h-1]; //Show the array item relevant to the hour 
}​ 
相關問題