npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timezonecomplete

v5.13.1

Published

DateTime, TimeZone, Duration and Period library aimed at providing a consistent and complete date-time interface, away from the original JavaScript Date class.

Downloads

71,592

Readme

TimezoneComplete

Synopsis

TimezoneComplete is a library of date/time utilities, all of which are aware of time zones and daylight saving time. It provides for calculating with Durations (amount of UTC milliseconds) and with Periods (regular intervals in some timezone's time, which might be irregular in UTC). It has aware DateTimes (with timezone) and unaware DateTimes (without timezone) and you are prevented from mixing the two in calculations.

Difference with timezone-js, Moment.js and other Date libraries

Other libraries are great, we had different requirements. The main thing for us was that calculations should be stable and predictable, especially around DST change moments. Above all, it should give the same answers across platforms. At the time we started this, none of the other libraries showed this level of predictability.

Try for example to convert some edge case timestamps back and forth between local and UTC or keep adding one hour to a timestamp through daylight saving time changes.

  • Consistent behaviour across platforms and with different TZ environment variable settings
  • Correct time zone conversions back and forth
  • Correct Daylight Saving Time handling with predictable behavior for non-existing hours during DST changes
  • Feature-rich:
    • DateTime class with or without time zone
    • Durations with units
    • Calculating with dates and durations across time zones
    • Periods with regular UTC or regular local time repetition
    • Utility functions for determining leap years, determining the last Monday of the month etc.
    • Formatting and parsing of dates using LDML format strings
    • Ability to use with Node.JS as well as in a browser (CommonJS, AMD)
  • Good test coverage
  • Under active development by a company who have an interest in keeping it up to date
  • Timezonecomplete is written in TypeScript and typings are included

New in Version 5

The TZ data is now installed as a separate NPM module tzdata. For browser use, the data is NOT automatically included anymore, to allow you to choose a subset of the data to optimize your bundle.

Separating the TZ data from timezonecomplete has three advantages:

  1. The data becomes useful to other modules than just timezonecomplete
  2. By choosing for e.g. 'tzdata-northamerica', you can install just the time zones you need, which is handy for browser use (smaller footprint).
  3. The upgrades to the TZ data become independent of changes to timezonecomplete. That means you do not have to upgrade to the latest timezonecomplete version to get the latest TZ data.

See the Upgrade Instructions.

Documentation

Usage

Node.JS

In Node.JS, timezonecomplete automatically has all time zone data.

Install using:

npm install timezonecomplete

Then require the timezonecomplete module in your code. Timezonecomplete will automatically find any installed tzdata modules:

var tc = require("timezonecomplete");

Browserify

If you use browserify, There are two options:

  1. Require the .json files in your code and pass them to TzDatabase.init() before using any timezonecomplete functionality.
  2. Include the tzdata .json files manually using browserify.require()

Option 1:

// browserify will pick these up. You may need to set the 'extensions' option of browserify to include .json files
// NOTE in TypeScript, also use 'const' NOT 'import'!
const northamerica = require('tzdata-northamerica');
const etcetera = require('tzdata-etcetera');
const tc = require('timezonecomplete');

// Do this before creating e.g. tc.DateTime objects.
// In this example we use only part of the timezone database to reduce bundle size.
// Note you could whittle down the zones and rules further if you like by e.g. excluding rules pre-1990.
// That's at your own risk though
tc.TzDatabase.init([northamerica, etcetera]);

Option 2:

// Manual browserifying ambient JSON data
var fs = require('fs');
var glob = require('glob');
var browserify  = require('browserify');
browserify({
    entries: glob.sync('./src/*.js'),
    extensions: ['.js', '.json'], // needed to include the tzdata modules
    debug: true
})
.require('./node_modules/tzdata/timezone-data.json', {expose: 'tzdata'}) // add 'tzdata' and make it available globally under its own name
.bundle()
.pipe(fs.createWriteStream('./dist/bundle.js'));

Webpack

Use a >=2.x (beta) version of webpack, to avoid warnings. Then, use a plugin in your webpack configuration to load the time zone data you need.

  plugins: [
    new webpack.ContextReplacementPlugin(
      /[\/\\]node_modules[\/\\]timezonecomplete[\/\\]/,
      path.resolve("tz-database-context"),
      {
        "tzdata-backward-utc": "tzdata-backward-utc",
        "tzdata-etcetera": "tzdata-etcetera",
        "tzdata-europe": "tzdata-europe"
      }
    )
  ]

To install a beta version of webpack, first, look up the latest unstable version using:

npm show webpack versions --json

Then, install the specific version by specifying it behind an @-sign (in this example 2.2.0-rc3) like this:

npm install --save-dev [email protected]

Browser, stand-alone

Timezonecomplete comes with a bundle and a minified bundle. Both are UMD (isomorphic) modules. Next to these bundles, you also need one or more of the bundles from the tzdata modules listed above.

You can find examples of using timezonecomplete in a browser in the examples directory, both using Require.JS and ambient.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Timezonecomplete Browser Example</title>
  <script src="./tzdata.js"></script>
  <script src="./timezonecomplete.min.js"></script>
  <script>
    function doIt() {
      var utc = tc.nowUtc();
      var local = utc.toZone(tc.zone('localtime'));
      var diff = local.toZone(null).diff(utc.toZone(null));
      var hourDiff = tc.hours(diff.hours());
      document.getElementById('diff').textContent = hourDiff.toString();
    }
  </script>
</head>
<body onLoad="doIt()">
  <p>
    The difference between local time and UTC is:&nbsp;
    <span id="diff"></div>
  </p>
</body>
</html>

License

MIT