admin管理员组

文章数量:1122832

I have a simple page that shows current time and date:

<p class="clock" id="clock-time"></p>
<h3 class="clock-date" id="clock-date"></h3>

And clock.js:

window.onload = updateClock;

function updateClock() {
    let dt = new Date();
    let time =
        dt.getHours() +
        ':' +
        (dt.getMinutes() < 10 ? '0' + dt.getMinutes() : dt.getMinutes()) +
        ':' +
        (dt.getSeconds() < 10 ? '0' + dt.getSeconds() : dt.getSeconds());
    let date =
        (dt.getDay() < 10 ? '0' + dt.getDay() : dt.getDay()) +
        '-' +
        (dt.getMonth() < 10 ? '0' + dt.getMonth() : dt.getMonth()) +
        '-' +
        dt.getFullYear();
    document.getElementById('clock-time').innerText = time;
    document.getElementById('clock-date').innerText = date;
    setTimeout(updateClock, 6000);
}

However it shows correct time, but not correct date: screenshot

I have a simple page that shows current time and date:

<p class="clock" id="clock-time"></p>
<h3 class="clock-date" id="clock-date"></h3>

And clock.js:

window.onload = updateClock;

function updateClock() {
    let dt = new Date();
    let time =
        dt.getHours() +
        ':' +
        (dt.getMinutes() < 10 ? '0' + dt.getMinutes() : dt.getMinutes()) +
        ':' +
        (dt.getSeconds() < 10 ? '0' + dt.getSeconds() : dt.getSeconds());
    let date =
        (dt.getDay() < 10 ? '0' + dt.getDay() : dt.getDay()) +
        '-' +
        (dt.getMonth() < 10 ? '0' + dt.getMonth() : dt.getMonth()) +
        '-' +
        dt.getFullYear();
    document.getElementById('clock-time').innerText = time;
    document.getElementById('clock-date').innerText = date;
    setTimeout(updateClock, 6000);
}

However it shows correct time, but not correct date: screenshot

Share Improve this question edited Nov 22, 2024 at 19:20 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Nov 22, 2024 at 14:37 msuny-cmsuny-c 12 bronze badges 2
  • Please post code, data, and results as text, not screenshots (how to format code in posts). Why should I not upload images of code/data/errors? idownvotedbecau.se/imageofcode – Barmar Commented Nov 22, 2024 at 19:44
  • This question is similar to: Date.getDay() javascript returns wrong day. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Jasper de Vries Commented Nov 23, 2024 at 9:10
Add a comment  | 

2 Answers 2

Reset to default 1

getDay should be getDate and getMonth is jan = 0 so +1 it

The issue with the date lies in how the getDay() and getMonth() methods are used:

getDay(): Returns the day of the week (0 for Sunday, 1 for Monday, etc.), not the day of the month. You should use getDate() instead to get the correct day of the month. getMonth(): Returns the month index (0 for January, 1 for February, etc.), so you need to add 1 to get the correct month number.

window.onload = updateClock;

function updateClock() {
    let dt = new Date();
    
    // Format the time
    let time =
        dt.getHours() +
        ':' +
        (dt.getMinutes() < 10 ? '0' + dt.getMinutes() : dt.getMinutes()) +
        ':' +
        (dt.getSeconds() < 10 ? '0' + dt.getSeconds() : dt.getSeconds());
    
    // Format the date
    let date =
        (dt.getDate() < 10 ? '0' + dt.getDate() : dt.getDate()) +
        '-' +
        (dt.getMonth() + 1 < 10 ? '0' + (dt.getMonth() + 1) : dt.getMonth() + 1) +
        '-' +
        dt.getFullYear();
    
    // Update the DOM elements
    document.getElementById('clock-time').innerText = time;
    document.getElementById('clock-date').innerText = date;
    
    // Update every second
    setTimeout(updateClock, 1000);
}

本文标签: javascriptWhy does new Date() show incorrect dateStack Overflow