Created
January 14, 2026 15:25
-
-
Save UnixSage/f68dfb1cd09866dd3c3c8567229f995c to your computer and use it in GitHub Desktop.
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>JavaScript Clock with Offset</title> | |
| </head> | |
| <body onload="startTime()"> | |
| <h2>Offset Clock</h2> | |
| <div id="txt" style="font-size: 24px;"></div> | |
| <script> | |
| function startTime() { | |
| const today = new Date(); | |
| // 1. Get the current UTC time in milliseconds | |
| const utc = today.getTime() + (today.getTimezoneOffset() * 60000); | |
| // 2. Define the desired offset in hours (e.g., 5.5 for UTC+5:30) | |
| // const offsetHours = 5.5; | |
| // const offsetMilliseconds = 3600000 * offsetHours; // 3600000ms in an hour | |
| const offsetSeconds=-600 | |
| const offsetMilliseconds = 1000 * offsetSeconds | |
| // 3. Create a new Date object with the applied offset | |
| const offsetDate = new Date(utc + offsetMilliseconds); | |
| let h = offsetDate.getHours(); | |
| let m = offsetDate.getMinutes(); | |
| let s = offsetDate.getSeconds(); | |
| m = checkTime(m); | |
| s = checkTime(s); | |
| document.getElementById('txt').innerHTML = h + ":" + m + ":" + s; | |
| // Update the clock every 1000 milliseconds (1 second) | |
| setTimeout(startTime, 1000); | |
| } | |
| // Function to add a leading zero to single-digit numbers | |
| function checkTime(i) { | |
| if (i < 10) { | |
| i = "0" + i; | |
| } | |
| return i; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment