• Home
  • /
  • Digital Clock with Live Time in JavaScript

Digital Clock with Live Time in JavaScript

HTML

				
					<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Digital Clock | Learn With Arshyan</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            height: 100vh;
            background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: 'Courier New', monospace;
        }

        .clock-container {
            background: rgba(255, 255, 255, 0.05);
            padding: 40px 80px;
            border-radius: 15px;
            box-shadow: 0 0 25px rgba(0, 0, 0, 0.4);
            border: 2px solid rgba(255, 255, 255, 0.1);
        }

        #clock {
            font-size: 3.5rem;
            color: #00ffe7;
            text-shadow: 0 0 10px #00ffe7, 0 0 20px #00ffe7;
            letter-spacing: 3px;
        }
    </style>
</head>

<body>
    <div class="clock-container">
        <h1 id="clock">00:00:00 AM</h1>
    </div>

    <script>
        function updateClock() {
            const now = new Date();
            let hours = now.getHours();
            const minutes = now.getMinutes();
            const seconds = now.getSeconds();
            const ampm = hours >= 12 ? "PM" : "AM";

            hours = hours % 12 || 12;

            const formatted = `${pad(hours)}:${pad(minutes)}:${pad(seconds)} ${ampm}`;
            document.getElementById("clock").textContent = formatted;
        }

        function pad(num) {
            return num < 10 ? "0" + num : num;
        }

        setInterval(updateClock, 1000);
        updateClock(); // run immediately

    </script>
</body>

</html>
				
			

Share this post

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top