minitz
TypeScript icon, indicating that this package has built-in type declarations

4.0.6 • Public • Published

minitz

Node.js CI Deno CI npm version NPM Downloads jsdelivr Codacy Badge MIT License

Minitz offers an efficient way to manage and manipulate time zone in native JavaScript Date objects. With a modern ES-module design and full typings, minitz is compatible with any environment that supports JavaScript or TypeScript. Highlights:

  • Convert dates between any timezone supported by the system.
  • Parses ISO8601 time strings.
  • MIT licensed, use the library any way you want. For real.
  • Minimal (less than 2 KB minified), no dependencies. Relies on JavaScript Intl and current best practices.
  • Works in Node.js >=14.0 (both require and import).
  • Works in Deno >=1.8.
  • Works in Bun >=0.2.2
  • Works in browsers as standalone, UMD or ES-module.
  • Includes TypeScript typings.

Try Minitz live on jsfiddle

Check out the full documentation at minitz.56k.guru

Usage

While converting a Date object to another timezone in JavaScript is achievable using the Intl feature of vanilla JS, things can get complicated when you want to convert date/time from another timezone or between different timezones. See the following examples:

Vanilla js for converting to a specific time zone:

// Get current time in Asia/Tokyo, using vanilla js
new Date().toLocaleString("sv-SE", { timeZone: "Asia/Tokyo" });
// -> 2022-09-15 17:23:45

Using minitz to convert from and across different time zones:

// Get local time from time in Asia/Tokyo, using minitz and vanilla js
const localTime = minitz(2022,9,15,23,0,0,"Asia/Tokyo")
console.log( localTime.toLocaleString("sv-SE") );
// -> 2022-09-15 16:00:00
// Get time in America/New_York from time in Asia/Tokyo, using minitz and vanilla js
// Also demonstrates that it's possible to use ISO8601 strings as input to minitz, 
// through `.fromTZISO`
const localTime = minitz.fromTZISO("2022-09-15 23:00:00","Asia/Tokyo");
console.log( localTime.toLocaleString("sv-SE", { timeZone: "America/New_York" }) );
// -> 2022-09-15 10:00:00

Installation

Details on installation for various platforms, including Node.js, Deno, Bun, and browsers, can be found in the full documentation. A few installation commands are:

Node.js: npm install minitz

Deno: import minitz from "https://deno.land/x/minitz@4.0.6/src/minitz.js";

Bun: bun add minitz

For CDN, refer to the documentation.

Contributing

Any contributions are welcome. See the Contribution guide to get started.

Donations

If you found this library helpful and wish to support its development, consider making a donation through Hexagon's GitHub Sponsors page. Your generosity ensures the library's continued development and maintenance.

License

MIT

Package Sidebar

Install

npm i minitz

Weekly Downloads

5

Version

4.0.6

License

MIT

Unpacked Size

20.9 kB

Total Files

8

Last publish

Collaborators

  • hexagon