|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Digital Clock</title> |
| 7 | + <style> |
| 8 | + /* Center the clock on the page with a nice design */ |
| 9 | + body { |
| 10 | + display: flex; |
| 11 | + justify-content: center; |
| 12 | + align-items: center; |
| 13 | + height: 100vh; /* full viewport height */ |
| 14 | + background: #111; /* dark background */ |
| 15 | + color: #00ffcc; /* neon green text */ |
| 16 | + font-family: monospace, Arial, sans-serif; |
| 17 | + font-size: 3rem; |
| 18 | + } |
| 19 | + |
| 20 | + /* Style the clock container */ |
| 21 | + #clock { |
| 22 | + padding: 20px; |
| 23 | + border: 2px solid #00ffcc; |
| 24 | + border-radius: 10px; |
| 25 | + box-shadow: 0 0 15px #00ffcc; |
| 26 | + } |
| 27 | + </style> |
| 28 | +</head> |
| 29 | +<body> |
| 30 | + <!-- The clock will appear inside this div --> |
| 31 | + <div id="clock"></div> |
| 32 | + |
| 33 | + |
| 34 | + <script> |
| 35 | + // Function to update the clock every second |
| 36 | + function updateClock() { |
| 37 | + const now = new Date(); // get current date and time |
| 38 | + |
| 39 | + // Get hours, minutes, and seconds |
| 40 | + let hours = now.getHours(); |
| 41 | + let minutes = now.getMinutes(); |
| 42 | + let seconds = now.getSeconds(); |
| 43 | + |
| 44 | + // Determine AM or PM |
| 45 | + const ampm = hours >= 12 ? 'PM' : 'AM'; |
| 46 | + |
| 47 | + // Convert 24-hour format to 12-hour format |
| 48 | + hours = hours % 12 || 12; // if hours = 0, set to 12 |
| 49 | + |
| 50 | + // Add leading zeros to minutes and seconds if needed |
| 51 | + minutes = minutes < 10 ? '0' + minutes : minutes; |
| 52 | + seconds = seconds < 10 ? '0' + seconds : seconds; |
| 53 | + |
| 54 | + // Format the time string |
| 55 | + const timeString = `${hours}:${minutes}:${seconds} ${ampm}`; |
| 56 | + |
| 57 | + // Display the time in the clock div |
| 58 | + document.getElementById('clock').textContent = timeString; |
| 59 | + } |
| 60 | + |
| 61 | + // Call updateClock every 1000 milliseconds (1 second) |
| 62 | + setInterval(updateClock, 1000); |
| 63 | + |
| 64 | + // Call it once immediately to show the clock right away |
| 65 | + updateClock(); |
| 66 | + </script> |
| 67 | +</body> |
| 68 | +</html> |
0 commit comments