function timeNow(i) {
var d = new Date(),
h = (d.getHours() < 10 ? '0' : '') + d.getHours(),
m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes();
i.value = h + ':' + m;
}
document.getElementById('dashboard-hello').innerHTML = timeNow();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dashboard-hello"></div>
&#13;
我正在尝试使用timeNow
功能获取当前时间,然后我希望它显示在我的#dashboard-hello
ID中。我不确定我做错了什么,但它并没有显示出来。如果我在尝试使用innerHTML时取出timeNow
中的(),则会打印整个函数
function timeNow(i) { var d = new Date(), h = (d.getHours() < 10 ? '0' : '') + d.getHours(), m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes(); i.value = h + ':' + m; }
有人看到我做错了吗?
ps - 当我得到时间工作时,我怎么能把它结构成一个if语句,当它是4:00-11:30时说&#34;早安&#34;,然后下午同样/晚上?
更新的代码:
function timeNow() {
var d = new Date(),
h = (d.getHours() < 10 ? '0' + d.getHours() : '') + d.getHours(),
m = (d.getMinutes() < 10 ? '0' + d.getMinutes() : '') + d.getMinutes();
// To check for a time range (between 4:: and 11:30 here):
var morningTime = (h >= 4 && h <= 11) && m <= 30 ? true : false;
var noonTime = (h >= 12 && h <= 17) && m <= 30 ? true : false;
var nightTime = (h >= 18 && h <= 3) && m <= 30 ? true : false;
console.log(morningTime);
if (morningTime == true) {
return "Good Morning";
}
else if (noonTime == true) {
return "Good Afterboon";
}
else if (nightTime == true) {
return "Good Evening";
}
/* h = (d.getHours() < 10 ? '0' : '') + d.getHours();
m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes();*/
//return h + ':' + m;
}
document.getElementById('dashboard-hello').innerHTML = timeNow() + ', name';
答案 0 :(得分:1)
您正在尝试将innerHTML
的{{1}}设置为函数的结果,因此函数必须返回一个值,而不是尝试在div
上设置value
属性。 div
(div
s没有值属性)。
function timeNow() {
var d = new Date(),
// When the hour or minute is less than 10, you want to return 0 plus the hour or minute.
// You were just returning 0.
h = (d.getHours() < 10 ? '0' : '') + d.getHours(),
m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes();
// If you are going to set something to a function, the function must return a value
return h + ':' + m;
}
'
document.getElementById('dashboard-hello').innerHTML = timeNow();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dashboard-hello"></div>
要检查时间是否在4:00到11:30之间,您需要进行一些额外的测试:
function timeNow() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
// To check for a time range (between 4:: and 11:30 here):
var specialTime = (h >= 4 && h <= 11) && m <= 30 ? true : false;
// To check for a time range (between 4:: and 11:30 here):
var morningTime = (h >= 4 && h <= 11) && m <= 30 ? true : false;
var noonTime = (h >= 12 && h <= 17) && m <= 30 ? true : false;
var nightTime = (h >= 18 && h <= 3) && m <= 30 ? true : false;
var greeting = "";
if (morningTime) {
greeting = "Good Morning";
} else if (noonTime) {
greeting = "Good Afternoon";
} else if (nightTime) {
greeting = "Good Evening";
}
h = (d.getHours() < 10 ? '0' : '') + d.getHours();
m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes();
// If you are going to set something to a function, the function must return a value
return h + ':' + m + " (" + greeting + ")";
}
document.getElementById('dashboard-hello').innerHTML = timeNow();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dashboard-hello"></div>
答案 1 :(得分:0)
修正了你的代码。如果要为HTML元素赋值,则需要从函数返回一个值。
function timeNow() {
var d = new Date(),
h = (d.getHours() < 10 ? '0' : '') + d.getHours(),
m = (d.getMinutes() < 10 ? '0' : '') + d.getMinutes();
return h + ':' + m;
}
document.getElementById('dashboard-hello').innerHTML = timeNow();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="dashboard-hello"></div>
答案 2 :(得分:0)
这是您的代码(sorta),包括上午/下午/晚间资格赛
function timeNow(i) {
var d = new Date();
var afternoon=new Date(d.getFullYear(),d.getMonth(),d.getDate(),11,30);
var evening =new Date(d.getFullYear(),d.getMonth(),d.getDate(),17,30);
var greeting=(d.getTime()>evening.getTime()?"Evening":(d.getTime()>afternoon.getTime()?"Afternoon":"Morning"));
return greeting + " " + ("00"+d.getHours()).slice(-2) + ":" + ("00"+d.getMinutes()).slice(-2);
}
document.getElementById('dashboard-hello').innerHTML = timeNow();