我想调整此脚本,以便它还可以显示星期几和日期。
(function () {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function startTime() {
var today = new Date(),
h = checkTime(today.getHours()),
m = checkTime(today.getMinutes());
document.getElementById('time').innerHTML = h + ":" + m;
t = setTimeout(function () {
startTime()
}, 500);
}
startTime();
})();
我虽然添加了getDay();
,但我不知道如何在功能中添加它来说实话。知道日期和日期必须在页面上自动更新,这将持续数天......(这是一个几乎总是保持开启的显示。)
有什么想法吗?
答案 0 :(得分:1)
dayNames = ["DOM", "LUN", "MAR", "MER", "GIO", "VEN", "SAB"],
dayNames[date.getDay()]
显示您当天的字符串。
看看这里看个人工作的例子 https://fiddle.sencha.com/#view/editor&fiddle/1kfh
答案 1 :(得分:1)
将月份和日期名称添加到数组,然后使用getDay()
,getDate()
,getMonth()
和getFullYear()
:
(function () {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function startTime() {
var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ],
days = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ]
var today = new Date(),
d = today.getDay(),
f = today.getDate(),
m = today.getMonth(),
y = today.getFullYear(),
h = checkTime(today.getHours()),
i = checkTime(today.getMinutes());
var val = days[d]+', '+f+' '+months[m]+' '+y+' - '+h+':'+i;
document.getElementById('time').innerHTML = val;
t = setTimeout(function () {
startTime()
}, 500);
}
startTime();
})();
&#13;
<div id="time"></div>
&#13;
答案 2 :(得分:0)
使用以下代码并进行检查。 HTML 强>
<div id="time"></div>
<div id="day"></div>
<强>的Javascript 强>
(function () {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function startTime() {
var today = new Date(),
ndays = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ]
h = checkTime(today.getHours()),
m = checkTime(today.getMinutes());
s = checkTime(today.getSeconds());
dn = today.getDay(),
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
var d=ndays[dn];
document.getElementById('day').innerHTML=d;
t = setTimeout(function () {
startTime()
}, 500);
}
startTime();
})();