我正在尝试创建一个显示不同文本行的网站,具体取决于小时(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 :(得分:1)
Javascript有一个Date类。
var hour = new Date().getHours();
if(...) {
// do something
}
此代码提取
if (hour>10 && hour<11)
无法使用小时,因为时间不能
答案 1 :(得分: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;
//...
}
你也可以将你的短语放在一个数组中(你可以从服务器获得):
var phrases = ["Hi!", "blah", "more blah", ...];
...然后你可以用以下代码替换上面的开关:
document.write("<b>" + phrases[hour] + "</b>");
您可能希望节省一些时间并使用一个框架,使不同浏览器的工作方式大致相同。 JQuery是我最喜欢的,但有很多。这样的库可以很容易地操作页面中的内容,从服务器获取JSON等等。
答案 2 :(得分:0)
乍一看:
o.datetime
格式错误的JS Date方法。new Date()
的实例以使用小时.getHours()
您可以在此处查找从o.datetime
How can I convert string to datetime with format specification in JavaScript?
使用time.getHours() > 12
代替time > 12
如果您使用http://www.mattkruse.com/javascript/date/中的getDateFromFormat()
,我认为转换将类似于E, dd MMM hh:mm:ss +0000
简单回答:
您只需将hour
解析为回调:(在此处查看您的JSON http://json-time.appspot.com/time.json?tz=GMT)
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>");
}
});
答案 3 :(得分: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
}