I’m trying to make a real time clock with this format (DD/MM/YYYY – HOUR:MIN:SEC) but it displays the wrong date.
PHOTO:
It should display the current date.
MY CODE
function display_c() {
var refresh = 1000; // Refresh rate in milli seconds
mytime = setTimeout('display_ct()', refresh)
}
function display_ct() {
var x = new Date()
var day = x.getDay()
var month = x.getMonth()
var year = x.getFullYear()
var hour = x.getHours()
var min = x.getMinutes()
var sec = x.getSeconds()
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
if (hour < 10) hour = "0" + hour;
if (min < 10) min = "0" + min;
if (sec < 10) sec = "0" + sec;
var x1 = day + "/" + month + "/" + year + " - " + hour + ":" + min + ":" + sec;
document.getElementById('ct').innerHTML = x1;
display_c();
}
<html>
<head>
<title>Lancelot</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="projetoNew.css">
<script type="text/javascript" src="projetoNew.js"></script>
</head>
<body onload=display_ct();>
<header>
<div class="headerContainer header-theme">
<a href="" class="header-logo button-theme1 header-item">Lancelot</a>
<span id="ct"></span>
</div>
</header>