Home

How do I use jsTimezoneDetect?

This script does one thing and one thing alone. It gives you the zone info key of a timezone that describes the timezone you are in.

It is synchronous, fast and about 1.65 KB gzipped (4.9 KB uncompressed).

Download the latest minified version. Include it on a webpage. Then:

var timezone = jstz.determine();
timezone.name(); 
"Europe/Berlin"

If you are interested in the code details, read About the Code.

Click here for a link to a list of timezones that the script can detect.

The latest version of jsTimezoneDetect is running on http://www.pageloom.com/automatic-timezone-detection-with-javascript.

Contact me: pellepim , if you have any questions.

What do I do with the result?

jsTimezoneDetect gives you a timezone key, compliant with the zoneinfo database. Often called the "Olson database", or simply the tz database. It is a public domain collaborative compilation of information about the world's timezones.

It is widely used as basis for timezone information in operating systems and software. Nearly all platforms and programming languages have libraries that support it.

Who would benefit from this script?

Programmers who want to store and work with server side representations of a user's timezone for the use of showing dates and time.

jsTimezoneDetect is built to give you a "good enough" timezone so that you will be able to present dates and times to an end user in the expected manner.

Simply put: you can use this script to get an Olson timezone key for your user's timezone, that's it. The Olson database is available on most platforms and for most programming languages.

Who does not benefit from this script?

Programmers who need a very exact timezone. jsTimezoneDetect is only good at finding a timezone that is good enough to display dates and times. It will not help you if you want to know what days are official holidays in a certain region.

The Olson Timezone Database

Currently this script only maps timezones to the Olson database (also known as the tz database or the zoneinfo database). To do anything sensible with the key you get from this script you will need to do a lookup in the Olson database.

For example, if you are using Python, you can use the module pytz to accomplish the following:

from datetime import datetime
from pytz import timezone
import pytz

def convert():
    """ 
    Converts 2010-12-25 06:00:00 UTC+0000 to Central European Time
    """
    utc = pytz.utc
    cet = timezone('Europe/Amsterdam') # Europe/Amsterdam is the tz key jsTimezoneDetect will hand you
    format = '%Y-%m-%d %H:%M:%S %Z%z'

    utc_datetime = utc.localize(datetime(2010, 12, 25, 6, 0, 0)) #2010-12-25 06:00:00 UTC+0000
    cet_datetime = utc_datetime.astimezone(cet)

    return cet_datetime.strftime(format) #returns 2010-12-25 07:00:00 CET+0100

A little about the Olson keys

Not all Olson tz keys are given by this script. Many are superfluous, e.g you don't need them to do normal datetime normalizations and calculations. If there are many timezones with UTC+6 but none of them use daylight savings, this script simply returns ONE of the timezone keys representing this type of timezones.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.