JS日期和时间不返回任何内容

时间:2019-09-03 09:17:43

标签: javascript date time clock

我创建了一个JavaScript以用Javascript显示日期和时间,但未返回任何内容 我尝试了很多建议,但没有任何效果。

HTML代码

function startTime() {
    var today = new Date();
    var hr = today.getHours();
    var min = today.getMinutes();
    var sec = today.getSeconds();
    ap = (hr < 12) ? "<span>AM</span>" : "<span>PM</span>";
    hr = (hr == 0) ? 12 : hr;
    hr = (hr > 12) ? hr - 12 : hr;
    //Add a zero in front of numbers<10
    hr = checkTime(hr);
    min = checkTime(min);
    sec = checkTime(sec);
    document.getElementById("clock").innerHTML = hr + ":" + min + ":" + sec + " " + ap;
    
    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    var curWeekDay = days[today.getDay()];
    var curDay = today.getDate();
    var curMonth = months[today.getMonth()];
    var curYear = today.getFullYear();
    var date = curWeekDay+", "+curDay+" "+curMonth+" "+curYear;
    document.getElementById("date").innerHTML = date;
    
    var time = setTimeout(function(){ startTime() }, 500);
}
function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
   
div {
	margin
    border-radius:15px;
    color:#ecf0f1;
    text-align:center;
    font-size:70px
}
    <div id="clock"></div>
    <div id="date"></div>

我可以更改此javascript代码以显示5个不同时区的时间吗?

2 个答案:

答案 0 :(得分:0)

为了正常工作,调用函数 startTime 很重要,因此请将此行添加到您的javasrcipt代码中。

startTime();

答案 1 :(得分:0)

啊,我忘了打电话。抱歉。

使用

window.onload = function () {

'use strict';

setInterval(startTime, 500);