minitz
Converting between different timezones in pure JavaScript is tricky. This minimal library tries to solve that problem in the simplest possible way.
Minitz can convert regular Date objects to/from any timezone supported by the system (Node/Deno/Browser). Compatible with both ESM or UMD/CommnJS.
- Convert Dates between to/from any timezone.
- MIT licensed, use the library any way you want. For real.
- Minimal, no dependencies. Relies on JavaScript Intl and current best practises.
- Works in Node.js >=14.0 (both require and import).
- Works in Deno >=1.8.
- Works in browsers as standalone, UMD or ES-module.
- Includes TypeScript typings.
Documentation
Full documentation available at hexagon.github.io/minitz.
Installation
Node.js
npm install minitz --save
JavaScript
// ESM Import ...
import minitz from "minitz";
// ... or CommonJS Require
const minitz = require("minitz");
TypeScript
Note that only default export is available in Node.js TypeScript, as the commonjs module is used internally.
import minitz from "minitz";
// ...
Deno
JavaScript
import minitz from "https://cdn.jsdelivr.net/gh/hexagon/minitz/src/minitz.js";
// ...
TypeScript
import { minitz } from "https://cdn.jsdelivr.net/gh/hexagon/minitz/src/minitz.js";
// ...
Browser
Manual
- Download latest zipball
- Unpack
- Grab
minitz.min.js
(UMD and standalone) orminitz.min.mjs
(ES-module) from the dist/ folder
CDN
To use as a UMD-module (stand alone, RequireJS etc.)
<script src="https://cdn.jsdelivr.net/npm/minitz/dist/minitz.min.js"></script>
To use as a ES-module
<script type="module">
import minitz from "https://cdn.jsdelivr.net/npm/minitz/dist/minitz.min.mjs";
// ... see usage section ...
</script>
Examples
Assuming you have imported minitz as described under ‘Installation’.
Convert local time to a specific timezone
const
// Step 1: Create a date object with current local time
localTime = new Date(),
// Step 2: Convert to local time in America/New_York
timeInNewYork = minitz.toTZ(localTime, "America/New_York");
console.log("Local time: ", localTime.toLocaleString("sv-SE"));
// -> Local time: 2022-09-09 21:23:30
console.log("Time in New York: ", timeInNewYork.toLocaleString("sv-SE"));
// -> Time in New York: 2022-09-09 15:23:30
Convert from a specific timezone to local time
const
// Step 1: Create a date object with local time in target timezone
timeInNewYork = new Date(Date.parse("2022-09-10 23:08:09")),
// Step 2: Convert to "real" local time using minitz.fromTZ
localTime = minitz.fromTZ(timeInNewYork, "America/New_York");
console.log("Time in New York: ", timeInNewYork.toLocaleString("sv-SE"));
// Time in New York: 2022-09-10 23:08:09
console.log("Local time: ", localTime.toLocaleString("sv-SE"));
// Local time (Europe/Stockholm): 2022-09-11 05:08:09
Contributing
Any contributions are welcome. See Contribution Guide
License
MIT