-
Notifications
You must be signed in to change notification settings - Fork 184
added live digital clock example #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65d8e38
added Live Digital Clock example
dumartins 0a52fe0
fixed responsiveness issue
dumartins 0691e83
enhanced styling
dumartins 09b14e7
updated README
dumartins a4ef1e2
made button text initialization consistent
dumartins 461e77e
fixed body background color
dumartins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Live Digital Clock | ||
|
|
||
| A responsive, beginner-friendly digital clock built with HTML, CSS, and JavaScript. It shows the current time in hours, minutes, and seconds, updates every second, and lets you toggle between 12-hour (AM/PM) and 24-hour formats. | ||
|
|
||
| ## How it works | ||
|
|
||
| - Uses the JavaScript `Date` object to read the current time. | ||
| - `setInterval()` runs every 1000ms to refresh the display so the clock stays accurate. | ||
| - Time values are padded with leading zeros for consistent formatting. | ||
| - A toggle button flips between 12-hour and 24-hour modes; the AM/PM badge switches to `24H` in 24-hour mode. | ||
| - DOM elements (`hours`, `minutes`, `seconds`, `ampm`) are updated on each tick, with an initial render before the interval starts. | ||
|
|
||
| ## Run it | ||
|
|
||
| No dependencies or API keys are needed. Just open `index.html` in your browser: | ||
|
|
||
| ```bash | ||
| cd examples/Live-Digital-Clock | ||
| open index.html | ||
| # or double-click index.html in your file explorer | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Live Digital Clock</title> | ||
| <link rel="preconnect" href="https://fonts.googleapis.com"> | ||
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
| <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet"> | ||
| <link rel="stylesheet" href="styles.css"> | ||
| </head> | ||
| <body> | ||
| <main class="page"> | ||
| <div class="clock-card"> | ||
| <header class="clock-header"> | ||
| <h1>Digital Clock</h1> | ||
| <p class="subtitle">Shows the current time with a 12/24-hour toggle using the JavaScript Date object.</p> | ||
| </header> | ||
|
|
||
| <section class="clock-face" role="timer" aria-live="polite"> | ||
| <div class="time-row"> | ||
| <div class="time-block"> | ||
| <span class="label">Hours</span> | ||
| <span class="value" id="hours">00</span> | ||
| </div> | ||
| <span class="colon">:</span> | ||
| <div class="time-block"> | ||
| <span class="label">Minutes</span> | ||
| <span class="value" id="minutes">00</span> | ||
| </div> | ||
| <span class="colon">:</span> | ||
| <div class="time-block"> | ||
| <span class="label">Seconds</span> | ||
| <span class="value" id="seconds">00</span> | ||
| </div> | ||
| <div class="ampm" id="ampm">AM</div> | ||
| </div> | ||
| </section> | ||
|
|
||
| <div class="controls"> | ||
| <button type="button" id="formatToggle" aria-pressed="false">Switch to 24-hour</button> | ||
| </div> | ||
| </div> | ||
| </main> | ||
|
|
||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| const hoursEl = document.getElementById("hours"); | ||
| const minutesEl = document.getElementById("minutes"); | ||
| const secondsEl = document.getElementById("seconds"); | ||
| const ampmEl = document.getElementById("ampm"); | ||
| const toggleBtn = document.getElementById("formatToggle"); | ||
|
|
||
| let use24HourFormat = false; | ||
|
|
||
| const pad = (value) => String(value).padStart(2, "0"); | ||
|
|
||
| const updateControls = () => { | ||
| toggleBtn.textContent = use24HourFormat ? "Switch to AM-PM format" : "Switch to 24-hour format"; | ||
| toggleBtn.setAttribute("aria-pressed", String(use24HourFormat)); | ||
| }; | ||
|
|
||
| const renderClock = () => { | ||
| const now = new Date(); | ||
| const hours24 = now.getHours(); | ||
| const minutes = pad(now.getMinutes()); | ||
| const seconds = pad(now.getSeconds()); | ||
|
|
||
| let displayHours = hours24; | ||
| let ampm = hours24 >= 12 ? "PM" : "AM"; | ||
|
|
||
| if (!use24HourFormat) { | ||
| displayHours = hours24 % 12 || 12; | ||
| } else { | ||
| ampm = "24H"; | ||
| } | ||
|
|
||
| hoursEl.textContent = pad(displayHours); | ||
| minutesEl.textContent = minutes; | ||
| secondsEl.textContent = seconds; | ||
| ampmEl.textContent = ampm; | ||
| }; | ||
|
|
||
| toggleBtn.addEventListener("click", () => { | ||
| use24HourFormat = !use24HourFormat; | ||
| updateControls(); | ||
| renderClock(); | ||
| }); | ||
|
|
||
| updateControls(); | ||
| renderClock(); | ||
| setInterval(renderClock, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'); | ||
|
|
||
| * { | ||
| box-sizing: border-box; | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| body { | ||
| min-height: 100vh; | ||
| font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; | ||
| background: edf2fb; | ||
dumartins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| color: #1f2a44; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 24px 16px; | ||
| } | ||
|
|
||
| .page { | ||
| width: min(960px, 100%); | ||
| display: flex; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .clock-card { | ||
| width: 100%; | ||
| background-color: rgba(202, 240, 248, 0.3); | ||
| border: 1px solid #e4e7ee; | ||
| border-radius: 14px; | ||
| padding: 28px 22px; | ||
| box-shadow: 0 12px 30px rgba(0, 0, 0, 0.05); | ||
| } | ||
|
|
||
| .clock-header { | ||
| text-align: center; | ||
| margin-bottom: 22px; | ||
| } | ||
|
|
||
|
|
||
| .clock-header h1 { | ||
| font-size: clamp(2.4rem, 4vw, 3rem); | ||
| color: #034078; | ||
| font-weight: 700; | ||
| margin-bottom: 4px; | ||
| } | ||
|
|
||
| .subtitle { | ||
| margin-top: 15px; | ||
| margin-bottom: 25px; | ||
| color: #034078; | ||
| font-size: 1rem; | ||
| line-height: 1.5; | ||
| } | ||
|
|
||
| .time-row { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 10px; | ||
| flex-wrap: nowrap; | ||
| width: 100%; | ||
| } | ||
|
|
||
| .time-block { | ||
| text-align: center; | ||
| color: #1282a2; | ||
| background-color: #ffffff; | ||
| border-radius: 10px; | ||
| padding: 10px 8px; | ||
| border: 1px solid #e4e7ee; | ||
| flex: 1 1 0; | ||
| min-width: 0; | ||
| } | ||
|
|
||
| .label { | ||
| display: block; | ||
| color: #034078; | ||
| font-size: 0.85rem; | ||
| font-weight: 500; | ||
| margin-bottom: 4px; | ||
| } | ||
|
|
||
| .value { | ||
| display: inline-block; | ||
| font-size: clamp(2rem, 5vw, 2.6rem); | ||
| font-weight: 700; | ||
| letter-spacing: 0.04em; | ||
| } | ||
|
|
||
| .colon { | ||
| text-align: center; | ||
| font-size: clamp(2rem, 5vw, 2.4rem); | ||
| font-weight: 700; | ||
| color: #81c3d7; | ||
| padding: 0 3px; | ||
| } | ||
|
|
||
| .ampm { | ||
| justify-self: end; | ||
| padding: 10px 12px; | ||
| border-radius: 10px; | ||
| background: #34a0a4; | ||
| color: #ffffff; | ||
| font-weight: 600; | ||
| border: 1px solid #d6e5ff; | ||
| letter-spacing: 0.05em; | ||
| flex: 0 1 auto; | ||
| } | ||
|
|
||
| .controls { | ||
| margin-top: 24px; | ||
| display: flex; | ||
| align-items: center; | ||
| gap: 16px; | ||
| flex-wrap: wrap; | ||
| justify-content: center; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .controls button { | ||
| background: #34a0a4; | ||
| color: #ffffff; | ||
| border: none; | ||
| border-radius: 10px; | ||
| padding: 12px 16px; | ||
| font-weight: 700; | ||
| cursor: pointer; | ||
| transition: transform 0.15s ease, box-shadow 0.15s ease; | ||
| box-shadow: 0 8px 18px rgba(37, 99, 235, 0.25); | ||
| } | ||
|
|
||
| .controls button:hover { | ||
| transform: translateY(-1px); | ||
| box-shadow: 0 10px 20px rgba(37, 99, 235, 0.28); | ||
| } | ||
|
|
||
| .controls button:active { | ||
| transform: translateY(0); | ||
| } | ||
|
|
||
| /* Responsive Design */ | ||
| @media (max-width: 680px) { | ||
| body { | ||
| padding: 16px; | ||
| } | ||
|
|
||
| .clock-card { | ||
| padding: 22px 16px; | ||
| } | ||
|
|
||
| .time-row { | ||
| flex-wrap: nowrap; | ||
| gap: 6px; | ||
| } | ||
|
|
||
| .time-block { | ||
| min-width: 0; | ||
| padding: 8px 6px; | ||
| flex: 1 1 0; | ||
| } | ||
|
|
||
| .value { | ||
| font-size: clamp(1.6rem, 8vw, 2.1rem); | ||
| } | ||
|
|
||
| .colon { | ||
| font-size: clamp(1.6rem, 8vw, 2.1rem); | ||
| padding: 0 2px; | ||
| } | ||
|
|
||
| .ampm { | ||
| width: auto; | ||
| text-align: center; | ||
| align-self: center; | ||
| padding: 8px 10px; | ||
| font-size: 0.85rem; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.