Skip to content

Auto update date and time [closed]

I would like to make the current date and time in a div element in html update automatically. Is there a way I can do this? I do know that I can update it by clicking on a button.

Answer

You can do this:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="time"></div>
<script>
    setInterval(function(){ document.getElementById('time').innerText = new Date()}, 1000);
</script>
</body>
</html>