Analytics choices

Analytics is optional. If allowed, Google measures public pages, navigation, recognized acquisition sources and engagement, including tool and playground use. The site excludes entered code, search terms and form contents from its Analytics events. You can withdraw here. This choice does not change advertising consent.

Read about Analytics and your information.

A good first building block

World time with a timezone selector

Choose a timezone and see its time and date using Intl.DateTimeFormat.

HTMLCSSJavaScriptNo dependencies
Edit in playground

Live preview

Explore the result below

HTML

<main
  ><h1>A window into another time</h1><label for="zone">Timezone</label
  ><select id="zone"
    ><option value="UTC">UTC</option
    ><option value="America/New_York">New York</option
    ><option value="Europe/London">London</option
    ><option value="Asia/Tokyo">Tokyo</option
    ><option value="Pacific/Auckland">Auckland</option></select
  ><p><time id="world-time">Loading time…</time></p
  ><p id="world-date"></p
  ><button id="pause-clock" aria-pressed="false">Pause updates</button
  ><p id="world-note">Time comes from your device clock.</p></main
>
Read the complete HTML document
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>My code example</title>
<style>
:root {
  font-family: system-ui, sans-serif;
  color: #18223b;
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  min-height: 100vh;
  padding: 24px;
  background: #edf1ff;
  display: grid;
  place-items: center;
}
main {
  width: min(100%, 480px);
  background: white;
  border-radius: 20px;
  padding: 24px;
}
h1 {
  font-size: 24px;
  margin: 0 0 16px;
}
button,
input,
select,
textarea {
  font: inherit;
}
button {
  min-height: 44px;
  border: 1px solid #afbddd;
  border-radius: 10px;
  padding: 10px 14px;
  background: #eff3ff;
  color: #18223b;
  cursor: pointer;
}
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid #3658bf;
  outline-offset: 3px;
}
p {
  line-height: 1.65;
}
label {
  display: block;
  margin: 16px 0 6px;
}
input:not([type="checkbox"]),
select,
textarea {
  max-width: 100%;
  width: 100%;
  padding: 10px;
  border: 1px solid #7a8496;
  border-radius: 8px;
}
button:disabled {
  opacity: 0.5;
  cursor: default;
}
time {
  display: block;
  font-size: clamp(30px, 8vw, 48px);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}
</style>
</head>
<body>
<main
  ><h1>A window into another time</h1><label for="zone">Timezone</label
  ><select id="zone"
    ><option value="UTC">UTC</option
    ><option value="America/New_York">New York</option
    ><option value="Europe/London">London</option
    ><option value="Asia/Tokyo">Tokyo</option
    ><option value="Pacific/Auckland">Auckland</option></select
  ><p><time id="world-time">Loading time…</time></p
  ><p id="world-date"></p
  ><button id="pause-clock" aria-pressed="false">Pause updates</button
  ><p id="world-note">Time comes from your device clock.</p></main
>
<script>
const zone = document.querySelector("#zone"),
  time = document.querySelector("#world-time"),
  date = document.querySelector("#world-date"),
  pause = document.querySelector("#pause-clock");
let paused = false;
function update() {
  if (paused || document.hidden) return;
  const now = new Date();
  try {
    time.textContent = new Intl.DateTimeFormat("en-GB", {
      timeZone: zone.value,
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
      hour12: false,
    }).format(now);
    time.dateTime = now.toISOString();
    date.textContent = new Intl.DateTimeFormat("en-GB", {
      timeZone: zone.value,
      weekday: "long",
      year: "numeric",
      month: "long",
      day: "numeric",
    }).format(now);
  } catch {
    time.textContent = "This timezone is not available in your browser.";
    date.textContent = "";
  }
}
zone.addEventListener("change", () => {
  const wasPaused = paused;
  paused = false;
  update();
  paused = wasPaused;
});
pause.addEventListener("click", () => {
  paused = !paused;
  pause.setAttribute("aria-pressed", String(paused));
  pause.textContent = paused ? "Resume updates" : "Pause updates";
  update();
});
document.addEventListener("visibilitychange", update);
setInterval(update, 1000);
update();
</script>
</body>
</html>

A little guide to making it yours

  1. Copy the code and save it in a file named example.html.
  2. Open the file in your browser to see the complete example.
  3. Change the text and structure in the HTML.
  4. Adjust the colors and spacing in the CSS, or experiment in the playground.

How it works

Intl.DateTimeFormat applies the browser timezone data, including daylight-saving rules; fixed hour offsets do not. The machine-readable time is the same UTC instant while the display uses the selected zone. This is a device-clock demonstration, not a server-synchronized clock. Updates pause while the document is hidden.

Historical purpose: Historical title/link recorded at this exact path. The archived implementation was not recovered; this is newly authored code for the named purpose.

Troubleshooting and limits

Intl formats supported IANA timezones using the device clock. Browser timezone data can vary; an unsupported zone should be handled before adding it. Pause stops updates, and refresh resumes them. It does not verify clock accuracy or contact a time service.

This is newly authored code. It uses system fonts and no external assets.

Keep the ideas going