如何使用Javascript在午夜或基本上在新的一天更新当前日期

时间:2019-06-12 08:21:28

标签: javascript html

所以我有网上观看什么应该24/7。它显示数字时间和当前日期。没有其他的。

我的问题是当午夜过后,当前日期不会更新,我需要重新启动整个应用程序。

我试图为showDate()函数添加setTimeout,但是它没有帮助我。我当时正在考虑每隔几天移动+1。还有什么更有效的吗?

function showdate(){

    var date = new Date();
    var day = date.getDay();

    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];

    document.getElementById("date").textContent = name;

}

showdate();

function showTime(){
    var date = new Date();
    var h = date.getHours(); // 0 - 23
    var m = date.getMinutes(); // 0 - 59
    var s = date.getSeconds(); // 0 - 59
    var session = "AM";

    if(h === 0){
        h = 24;
    }

    /*if(h > 12){
        h = h - 12;
        session = "PM";
    }*/

    h = (h < 10) ? "0" + h : h;
    m = (m < 10) ? "0" + m : m;
    s = (s < 10) ? "0" + s : s;

    var time = h + ":" + m + ":" + s;
    /*var sess = session;*/
    document.getElementById("MyClockDisplay").innerText = time;
    document.getElementById("MyClockDisplay").textContent = time;
   /* document.getElementById("PM").textContent = sess;*/

    setTimeout(showTime, 1000);     

   }

showTime();

谢谢你的想法。

3 个答案:

答案 0 :(得分:1)

您的代码不会自动执行。您应该像这样定期调用它:

function showdate(){

    var date = new Date();

    var day = date.getDay();

    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];

    document.getElementById("date").textContent = name;

        console.log('show date');
}

setInterval(showdate, 1000)

这样,客户端将每秒获得新日期(其中1000以毫秒为单位)。但是您可以将时间更改为适合您需要的任何值(请记住,它在ms中)。

您还可以查看this example

答案 1 :(得分:1)

function showdate(){
    var date = new Date();
    var day = date.getDay();
    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];
    document.getElementById("date").textContent = name;

		console.log('show date');
}
setInterval(showdate, 1000)

function checkTime(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
}

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    // add a zero in front of numbers<10
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
    t = setTimeout(function() {
      startTime()
    }, 500);
}
startTime();
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="date"></div>
<div id="time"></div>

</body>
</html>

答案 2 :(得分:1)

您可以这样做:

// Show date every day at 12
function showdateEveryDayAt12() {
  setInterval(function() {
    showdate();
  }, 86400000);
}

// Show date at next 12, and call showdateEveryDayAt12()
function showdateAtNext12() {
  var now = new Date();
  var millisecondsTo12 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 12, 0, 0, 0) - now;

  setTimeout(function() {
    showdate();
    showdateEveryDayAt12();
  }, millisecondsTo12);
}

// Show date
function showdate() {
  var date = new Date();
  var day = date.getDay();
  var name = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][day];

  document.getElementById("date").textContent = name;
}


// Show date now, at next 12 and every day at 12
showdate();
showdateAtNext12();
<p id="date"></p>