+1 vote
120 views
by (98.9k points)
Write a JavaScript code for displaying a digital clock on a web page.

1 Answer

0 votes
by (98.9k points)
 
Best answer
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Digital Clock</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            text-align: center;
            margin: 100px;
            font-size: 2em;
        }
    </style>
</head>
<body>

<div id="digitalClock"></div>

<script>
    function updateClock() {
        const now = new Date();
        const timeString = now.toTimeString().slice(0, 8);
        document.getElementById("digitalClock").innerHTML = timeString;
    }

    setInterval(updateClock, 1000);
    updateClock(); // Display the clock immediately
</script>

</body>
</html>
Output

Related questions

+1 vote
1 answer 315 views
+1 vote
1 answer 134 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

561 users

...