Attributes
Includes Deno configuration
Repository
Current version released
2 years ago
minitz
Converting Date objects between different timezones in javascript is hard. This minimal library tries to solve that problem in the simplest possible way. You can convert dates to a specific timezone, and you can also convert dates from specific timezones.
- Convert Dates between timezones.
- 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.
- Important: Currently in pre-release. Check back for version 1.0 later.
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>
Documentation
Full documentation available at hexagon.github.io/minitz.
Examples
Assuming you have imported minitz as described under ‘Installation’.
Convert local time to a specific timezone
const
localTime = new Date(),
timeInSantiago = minitz.toTZ(localTime, "America/Santiago");
console.log("Local time: ", localTime.toLocaleString('sv-SE'));
// Local time: 2022-09-08 00:08:09
console.log("Time in Santiago: ", timeInSantiago.toLocaleString('sv-SE'));
// Time in santiago: 2022-09-07 19:08:09
Convert from a specific timezone to local time
const
timeInSantiago = new Date(Date.parse("2022-09-07 19:08:09")),
localTime = minitz.fromTZ(timeInSantiago, "America/Santiago");
console.log("Time in Santiago: ", timeInSantiago.toLocaleString('sv-SE'));
// Time in santiago: 2022-09-07 19:08:09
console.log("Local time: ", localTime.toLocaleString('sv-SE'));
// Local time: 2022-09-08 00:08:09
Contributing
License
MIT