Timezone Offset Utility

0.0.2 · abandoned · verified Sun Apr 19

This package, `tz-offset`, provides basic utilities for working with JavaScript timezone offsets. Its current and only stable version is 0.0.2, published many years ago, indicating it is no longer actively maintained. The library calculates offsets based on what is likely a static, outdated internal dataset, meaning it does not receive updates for new timezone rules, historical changes, or changes in Daylight Saving Time (DST) definitions. While it offers a simple API to get offsets, remove offsets, or represent dates in other timezones, its core functionality is severely limited by its lack of maintenance and reliance on potentially inaccurate data. Key differentiators, if any, are its minimal footprint and extreme simplicity, but these come at the significant cost of accuracy and robustness compared to modern alternatives that integrate with IANA timezone databases or leverage native browser APIs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates getting timezone offsets, removing local offsets, and converting a date to another timezone using the `tz-offset` package. Highlights its CommonJS nature.

/**
 * @typedef {import('tz-offset')} TZOffset
 */

/**
 * A basic example demonstrating the use of tz-offset for timezone calculations.
 * Note: This package is very old and likely abandoned. Consider modern alternatives.
 */
function demonstrateTzOffset() {
  // For CommonJS environments (Node.js)
  /** @type {TZOffset} */
  const tzOffset = require('tz-offset');

  // Get the offset for a specific timezone (e.g., America/Sao_Paulo)
  // Returns the offset in minutes. Sao Paulo is UTC-3, so 3 * 60 = 180.
  // Note: This value might not account for DST accurately in a historical context.
  const saoPauloOffset = tzOffset.offsetOf("America/Sao_Paulo");
  console.log(`Offset for America/Sao_Paulo: ${saoPauloOffset} minutes`); // Expected: 180

  // Remove the timezone offset from a given date
  const now = new Date();
  const dateWithoutOffset = tzOffset.removeOffset(now);
  console.log(`Original Date: ${now.toISOString()}`);
  console.log(`Date without local offset: ${dateWithoutOffset.toISOString()} (Note: This creates a new Date object representing the same 'wall-clock' time but in UTC, effectively removing the local offset).`);

  // Represent a given date in another timezone
  const referenceDate = new Date('2023-10-27T10:00:00Z'); // A UTC date
  const londonTime = tzOffset.timeAt(referenceDate, "Europe/London");
  console.log(`Reference Date (UTC): ${referenceDate.toISOString()}`);
  console.log(`Equivalent time in Europe/London: ${londonTime.toISOString()}`); // Will show the date as if it were in London's timezone
}

demonstrateTzOffset();

view raw JSON →