A world clock shows the time in several cities at once — useful for remote teams, travellers, and anyone with friends in another timezone. It is a great beginner project because it teaches the one JavaScript feature most developers get wrong at first: timezones.
The trick is Intl.DateTimeFormat with a timeZone option. You do not need a library and you do not need to calculate offsets yourself — the browser already knows every timezone, including daylight saving rules:
const cities = [
{ name: "New Delhi", zone: "Asia/Kolkata" },
{ name: "London", zone: "Europe/London" },
{ name: "New York", zone: "America/New_York" },
{ name: "Tokyo", zone: "Asia/Tokyo" },
{ name: "Sydney", zone: "Australia/Sydney" },
];
function formatTime(zone) {
return new Intl.DateTimeFormat("en-GB", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
timeZone: zone,
}).format(new Date());
}
Rendering is a loop that builds one card per city and refreshes all of them once a second. Each card also shows the date, because the date is often different from yours in another timezone — it can be tomorrow in Tokyo while it is still today in New York:
function renderClocks() {
const container = document.getElementById("clocks");
container.innerHTML = "";
cities.forEach((city) => {
const date = new Intl.DateTimeFormat("en-GB", {
weekday: "short",
day: "numeric",
month: "short",
timeZone: city.zone,
}).format(new Date());
const card = document.createElement("div");
card.className = "clock-card";
card.innerHTML = `
<h3>${city.name}</h3>
<p class="time">${formatTime(city.zone)}</p>
<p class="date">${date}</p>`;
container.appendChild(card);
});
}
setInterval(renderClocks, 1000);
renderClocks();
Letting the user add their own city is a nice next step. A select dropdown with a few dozen IANA timezone names is enough — the browser validates them, so an invalid zone simply throws a RangeError you can catch and show as a friendly message:
function addCity(name, zone) {
try {
new Intl.DateTimeFormat("en-GB", { timeZone: zone }).format(new Date());
} catch (e) {
alert("That timezone is not recognised.");
return;
}
cities.push({ name, zone });
localStorage.setItem("world-clock-cities", JSON.stringify(cities));
renderClocks();
}
One more useful feature is a meeting planner: pick a time in your timezone and see what time that is in every other city. The maths is the same — format the same Date object in each timezone — which is why the Intl approach is so much safer than adding and subtracting hour offsets by hand. Offsets change with daylight saving; the browser's timezone database does not lie.
Ways to extend it: highlight cities where it is currently night, add a search box that filters the city list, or store the user's cities in localStorage so their clock survives a refresh. Timezone handling is one of those skills that looks trivial until a bug costs a team a meeting — learning it on a small project like this pays off for years.
Top comments (0)