Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does new Date() show incorrect date?

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <h:outputScript name="clock.js"></h:outputScript>
    </h:head>
    <h:body>
        <p class="clock" id="clock-time"></p>
        <h3 class="clock-date" id="clock-date"></h3>
    </h:body>
</html>

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading