Course contentsShow
← Web Development
Lesson 2751 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

Detecting User Timezone

Learn how to detect the user's timezone using Intl.DateTimeFormat().resolvedOptions().timeZone.

Detecting User Timezone

You'll learn how to programmatically discover which timezone the user's browser is operating in, so you can display times appropriately for their location.

The Core Problem

You've learned to display dates in specific timezones using Intl.DateTimeFormat with the timeZone option, but which timezone should you use? Hard-coding "America/New_York" works for some users, but fails for everyone else. You need to detect what timezone the user's device is actually configured to use.

The Solution: resolvedOptions()

The Intl.DateTimeFormat object has a method called resolvedOptions() that returns an object containing the actual settings being used by the formatter—including the user's timezone.

const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

console.log(userTimezone);
// Example output: "America/Chicago"
// Example output: "Europe/London"
// Example output: "Asia/Tokyo"

This returns an IANA timezone identifier (like "America/Los_Angeles" or "Europe/Paris"), which you can then use when formatting dates for display or storing alongside user preferences.

Practical Usage

// Detect user timezone
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;

// Use it to format a date in their local time
const formatter = new Intl.DateTimeFormat('en-US', {
  timeZone: tz,
  dateStyle: 'full',
  timeStyle: 'long'
});

console.log(formatter.format(new Date()));
// Shows date/time formatted for the user's actual timezone

Think of It Like This

It's like asking someone "What city are you calling from?" instead of assuming everyone is in the same place as you.

Key Takeaway: Use Intl.DateTimeFormat().resolvedOptions().timeZone to detect the user's browser timezone and display dates in their local context.