Javascript日期时间两个小时

时间:2014-07-12 03:13:54

标签: javascript html5 time

我的网页设置了javascript代码,但日期时间总是两个小时。如果有人知道什么是错的,请帮忙。

以下是我的相关JavaScript代码:

function show() {
    var Digital = new Date()
    var hours = Digital.getHours()
    var minutes = Digital.getMinutes()
    var seconds = Digital.getSeconds()
    var dn = "AM"
    if (hours > 12) {
        dn = "PM"
        hours = hours - 12
    }
    if (hours == 0) c
    hours = 12
    if (minutes <= 9)
        minutes = "0" + minutes
    if (seconds <= 9)
        seconds = "0" + seconds
    document.dform.currenttime.value = hours + ":" + minutes + ":" + seconds + " " + dn
    setTimeout("show()", 1000)
}
show();

1 个答案:

答案 0 :(得分:3)

您的c就在这里:

if (hours==0)c

删除c。有用。不客气。

在您寻求帮助之前,我建议proofreading您的代码,但更重要的是,您应该格式化代码,使其足够清晰,以便校对。举个例子:

function show() {
    var Digital = new Date();
    var hours = Digital.getHours();
    var minutes = Digital.getMinutes();
    var seconds = Digital.getSeconds();
    var dn = "AM";

    if(hours > 12) {
        dn = "PM";
        hours -= 12;
    }

    if(hours == 0) hours = 12;
    if(minutes <= 9) minutes = "0" + minutes;
    if(seconds <= 9) seconds = "0" + seconds;

    document.dform.currenttime.value = hours + ":" + minutes + ":" + seconds + " " + dn;
}

var clock = setInterval(show, 1000);

这更容易阅读,您可能会注意到错误的c