Skip to content

Get the current time from an API in JavaScript (browser and Node.js)

Fetch the real current time in JavaScript instead of trusting the device clock: browser and Node.js fetch examples, server clock offset, zones and DST.

Updated

Short answer: new Date() tells you what the user's device thinks the time is. To get the real current time in JavaScript, call a time API with fetch, read unix_ms from the response, and keep the offset between that value and Date.now().

const res = await fetch("https://api.timelogicapi.com/v1/time/current?tz=UTC", {
  headers: { Authorization: `Bearer ${process.env.TIMELOGIC_API_KEY}` },
});
const now = await res.json();
console.log(now.utc, now.unix_ms); // "2026-10-30T16:22:54Z" 1793377374673

What does the JSON time API response look like?

TimeLogic API is a JSON time API: GET /v1/time/current returns one flat JSON object, so await res.json() gives you every field at once. Use unix_ms for logic and comparisons, utc as the canonical instant, and iso_local or human for display. This is the response for GET /v1/time/current?tz=America/New_York:

{
  "unix": 1793377374,
  "unix_ms": 1793377374673,
  "utc": "2026-10-30T16:22:54Z",
  "iso_local": "2026-10-30T12:22:54-04:00",
  "rfc2822": "Fri, 30 Oct 2026 12:22:54 -0400",
  "human": "October 30, 2026, 12:22 PM America/New_York",
  "day_number": 6,
  "day_short": "Fri",
  "day_full": "Friday",
  "timezone": "America/New_York"
}

Why not just use new Date()?

Date.now() and new Date() read the operating system clock. Users change it, it drifts on devices without network time, and a page cannot tell. Countdown timers, auctions, trial expiry checks, exam timers and "posted 3 minutes ago" labels all break when the clock is off. A time API gives every client the same reference instant.

How do I get the current time in Node.js?

Node 18 and later ship fetch, so the snippet above runs as-is. Keep the key in an environment variable:

// node time.mjs  (TIMELOGIC_API_KEY set in the environment)
const res = await fetch("https://api.timelogicapi.com/v1/time/current?tz=America/New_York", {
  headers: { "X-API-Key": process.env.TIMELOGIC_API_KEY },
  signal: AbortSignal.timeout(5000),
});
if (!res.ok) throw new Error(`TimeLogic API ${res.status}`);
const { iso_local, timezone, day_full } = await res.json();
console.log(iso_local, timezone, day_full); // 2026-10-30T12:22:54-04:00 America/New_York Friday

There is also an official JavaScript/TypeScript SDK if you prefer typed calls.

How do I get server time in the browser and keep it in sync?

Fetch once, measure the round trip, and store an offset. Then derive the corrected time locally on every tick:

// Runs in the browser against your own endpoint, which calls TimeLogic with the key server-side
// and returns its JSON (for example: /api/now -> GET /v1/time/current?tz=UTC).
async function syncClock() {
  const t0 = Date.now();
  const res = await fetch("/api/now", { cache: "no-store" });
  const t1 = Date.now();
  const { unix_ms } = await res.json();
  return unix_ms - (t0 + t1) / 2; // ms to add to Date.now()
}

const offset = await syncClock();
const trustedNow = () => new Date(Date.now() + offset);
setInterval(() => {
  document.querySelector("#clock").textContent = trustedNow().toLocaleTimeString();
}, 1000);

The midpoint trick assumes the request and response took about the same time; with a warm connection the error is a small fraction of the round trip. Re-sync every few minutes, or when the tab becomes visible again.

How do I show the time in another time zone?

Ask for the zone you need and the API applies the current offset and daylight saving rules:

const res = await fetch("https://api.timelogicapi.com/v1/time/current?tz=Asia/Tokyo,Europe/London,America/Los_Angeles", {
  headers: { Authorization: `Bearer ${process.env.TIMELOGIC_API_KEY}` },
});
for (const city of await res.json()) console.log(city.timezone, city.iso_local);

A comma-separated tz is a bulk request: up to 10 zones, one call, results in input order. To find the user's zone from their IP address, see Get the timezone from an IP address.

How do I convert a Unix timestamp to a date in JavaScript?

new Date(unix * 1000) (or new Date(unix_ms)) gives a Date; format it with toISOString() or Intl.DateTimeFormat. To convert a timestamp into a specific zone with DST applied, or to format it with strftime, use /v1/time/convert:

const url = "https://api.timelogicapi.com/v1/time/convert?unix=1711300000&tz=America/New_York";
const res = await fetch(url, { headers: { "X-API-Key": process.env.TIMELOGIC_API_KEY } });
console.log((await res.json()).iso_local); // 2024-03-24T13:06:40-04:00

Can I call it from a WorldTimeAPI-era script?

Yes. Old code that fetched worldtimeapi.org/api/timezone/... works with the host changed to api.timelogicapi.com and a key added; see the migration guide.

Related guides

Frequently asked questions

How do I get the current time from the internet in JavaScript?

Call a time API with fetch and read the timestamp from the JSON, for example GET https://api.timelogicapi.com/v1/time/current?tz=UTC returns unix_ms, utc and iso_local. new Date() only reads the device clock, which can be wrong.

How do I get the server time in JavaScript instead of the user's clock?

Fetch it once, note when the request left and when the answer came back, and keep the difference between the API's unix_ms and the midpoint of that round trip. Add that offset to Date.now() whenever you need the corrected time, so you do not call the API on every tick.

Can JavaScript in the browser call the TimeLogic API directly?

CORS allows it, but any key in browser code is visible to everyone who opens the page. For public sites, call your own backend and let it add the key.

Can JavaScript use NTP?

Not from a browser: NTP runs over UDP port 123 and browsers only speak HTTP(S) and WebSockets. An HTTP time API is the practical way to get trusted time in a web page; Node.js can use NTP packages, or the same HTTP API.

How do I get the current time in another time zone in JavaScript?

Pass the IANA zone to the API (tz=Asia/Tokyo) and read iso_local, which includes the offset, or format a corrected timestamp locally with Intl.DateTimeFormat and the timeZone option.