user time zone
time zone detection
programming guide
geolocation
time zone API

How to get a user's time zone?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

The best way to get a user's time zone depends on where your code runs. In a browser, the most reliable first step is to ask the client runtime directly for its configured IANA time zone; on mobile or desktop apps, use the operating system API; and on the server, avoid guessing from IP unless you truly have no better signal.

Prefer Client-Reported Time Zones

If you control the client, ask the client. That gives you the user's actual configured time zone instead of an approximation based on network location.

In a browser, the standard JavaScript answer is:

javascript
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timeZone);

Typical results look like America/New_York, Europe/Berlin, or Asia/Tokyo. These are IANA time zone identifiers, which are exactly what most backend and scheduling systems want.

This is generally better than inferring the time zone from latitude, longitude, or IP because it reflects the environment the user is actually using.

Send the Time Zone to the Server

Once the client knows its time zone, store it with the user's profile or send it with relevant requests.

javascript
1fetch("/api/profile/timezone", {
2  method: "POST",
3  headers: { "Content-Type": "application/json" },
4  body: JSON.stringify({ timeZone }),
5});

On the server, persist the IANA value as plain text. That allows you to convert times accurately later without re-detecting anything on each request.

The key design rule is simple: detect once from the client, store explicitly, and reuse it.

Native App Examples

Native platforms usually expose the current system time zone directly.

Swift:

swift
1import Foundation
2
3let timeZone = TimeZone.current.identifier
4print(timeZone)

Python:

python
from datetime import datetime

print(datetime.now().astimezone().tzinfo)

These APIs are preferable to location-based inference because they reflect the user's configured device setting, which is what matters for local display and scheduling.

Why IP and Geolocation Are Weak Fallbacks

Server-side detection from IP address can be useful when you have no client code or when you want a best-effort default. But it is not the same as knowing the user's real time zone.

IP-based detection fails or becomes ambiguous when:

  • the user is behind a VPN
  • the user is traveling but has not changed device settings
  • the IP maps only to a region with multiple zones
  • your server sees a proxy or corporate egress address

That is why IP-based time zone detection should be treated as a fallback suggestion, not authoritative data.

Distinguish Locale From Time Zone

Another common mistake is confusing locale and time zone. A locale such as en-US describes formatting preferences. It does not tell you whether the user is in New York, Phoenix, or Honolulu.

For example, these are separate concerns:

  • locale: en-US
  • time zone: America/Los_Angeles

You need the time zone specifically if you want correct local wall-clock conversion.

Store Time Zone, Not Just Offset

Do not store only a UTC offset such as -05:00. Offsets change with daylight saving rules and do not uniquely identify a region.

For scheduling, this is the safe pattern:

  • store timestamps in UTC
  • store the user's IANA time zone identifier
  • convert to local time only for display or scheduling logic

That keeps historical and future conversions correct even when daylight saving transitions are involved.

Common Pitfalls

  • Guessing from IP when client code is available gives you a weaker answer than simply asking the runtime for its configured time zone.
  • Storing only a UTC offset loses daylight saving rules and regional identity.
  • Confusing locale with time zone leads to wrong assumptions about local time behavior.
  • Re-detecting the time zone on every request instead of storing the user's chosen or reported value creates unnecessary inconsistency.
  • Assuming the detected time zone is a perfect statement of physical location is incorrect. It reflects configuration, not guaranteed geography.

Summary

  • Ask the client runtime for the time zone whenever possible.
  • In browsers, Intl.DateTimeFormat().resolvedOptions().timeZone is the standard answer.
  • Store the IANA time zone identifier explicitly with the user profile.
  • Use IP-based detection only as a fallback, not as the primary source of truth.
  • Keep timestamps in UTC and convert using the stored time zone when you need local time.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.