timezone functions using a geojson file with global timezones
Find a file
Dude named Ben 0af021e81e Modernize
2025-12-09 10:55:11 +01:00
assets npm package, first try 2017-05-02 18:16:37 +02:00
dist Modernize 2025-12-09 10:55:11 +01:00
test Modernize 2025-12-09 10:55:11 +01:00
.gitignore npm package, first try 2017-05-02 18:16:37 +02:00
.travis.yml Travis 2017-05-03 14:28:05 +02:00
example.md Modernize 2025-12-09 10:55:11 +01:00
index.ts Modernize 2025-12-09 10:55:11 +01:00
package-lock.json Modernize 2025-12-09 10:55:11 +01:00
package.json Modernize 2025-12-09 10:55:11 +01:00
README.md Modernize 2025-12-09 10:55:11 +01:00
timezone-data.ts Modernize 2025-12-09 10:55:11 +01:00
tsconfig.json Modernize 2025-12-09 10:55:11 +01:00

tz-geo

Features

  • Universal: Works in both Node.js and browser environments
  • TypeScript Support: Full type definitions included
  • Moment.js Integration: Built on moment-timezone for robust date/time handling
  • Self-Contained: Embedded GeoJSON data - no external file dependencies
  • ES Modules: Modern import/export syntax

Install

npm install tz-geo

Quick Start

See example.md for browser usage examples.

Usage

Node.js

    var tzgeo = require('tz-geo')
    var name         = tzgeo.tz(47.650499, -122.350070)                                // 'America/Los_Angeles'
    var now          = tzgeo.tzMoment(47.650499, -122.350070)                          // moment-timezone obj
    var specificTime = tzgeo.tzMoment(47.650499, -122.350070, '2016-03-30T01:23:45Z')  // moment-timezone obj

    //Returns an error or timezone string via callback
    tzgeo.getTimezone({ type: 'Point', coordinates: [51.48513770164579, 5.232168700000033 ] }, function(err, result){
      if (err) throw err;
      console.log(result.name);
    });

    //Returns an error or moment-timezone via callback
    tzgeo.getMoment({ type: 'Point', coordinates: [51.48513770164579, 5.232168700000033 ] }, function(err, result){
      if (err) throw err;
      console.log(result.name);
    });

Browser with Bundler

Since the library is built as CommonJS for Node.js compatibility, use a bundler like Webpack, Rollup, or Vite for browser usage:

import { tz, tzMoment, getTimezone, getMoment } from 'tz-geo';

const name = tz(47.650499, -122.350070); // 'America/Los_Angeles'
const now = tzMoment(47.650499, -122.350070); // moment-timezone obj

// Async timezone lookup with merged MomentZone data
getTimezone({ type: 'Point', coordinates: [51.48513770164579, 5.232168700000033 ] }, (err, result) => {
  if (err) throw err;
  console.log(result.name);        // IANA timezone name
  console.log(result.abbrs);       // Array of abbreviations
  console.log(result.offsets);     // Array of offsets
  console.log(result.time_zone);   // UTC format like "UTC+02:00"
  console.log(result.abbr(1640995200000)); // Get abbreviation for timestamp
});

Note: The library includes embedded timezone data (~5.7MB), so it's completely self-contained and works without any external dependencies or network requests.

TypeScript

import { tz, tzMoment, getTimezone, Point } from 'tz-geo';

const coordinates: Point = { type: 'Point', coordinates: [-122.350070, 47.650499] };
const timezoneName: string = tz(47.650499, -122.350070);
const momentObj = tzMoment(47.650499, -122.350070, '2023-07-01T12:00:00Z');

API Docs:

.tz(lat, lon)

Returns timezone name found at lat, lon. Returns null if timezone could not be found at coordinate.

.tzMoment(lat, lon, [dateTime])

Returns a moment-timezone object found at lat, lon. Returns null if timezone could not be found at coordinate. If dateTime is omitted, the moment-timezone will have the current time set. If dateTime is provided, moment-timezone will be set to the time provided according to the timezone found. dateTime can be any single-argument parameter that will get passed to the moment() parser.

.getTimezone(point, callback)

Returns a timezone string found at {type: 'point', coordinates: [lon, lat]}. Returns err if timezone could not be found or no moment-timezone could be constructed from the geojson. If no moment-timezone can be constructed, please let us know so we can add it to our datafile.

.getMoment(point, callback)

Returns a moment-timezone object found at {type: 'point', coordinates: [lon, lat]}. Returns err if timezone could not be found or no moment-timezone could be constructed from the geojson. If no moment-timezone can be constructed, please let us know so wee can add it to our datafile.