-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dev dependencies. Update docs.
- Loading branch information
Showing
119 changed files
with
624 additions
and
11,287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
[![npm version](https://badge.fury.io/js/minitz.svg)](https://badge.fury.io/js/minitz) [![NPM Downloads](https://img.shields.io/npm/dm/minitz.svg)](https://www.npmjs.org/package/minitz) [![jsdelivr](https://data.jsdelivr.com/v1/package/gh/hexagon/minitz/badge?style=rounded)](https://www.jsdelivr.com/package/gh/hexagon/minitz) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/4978bdbf495941c087ecb32b120f28ff)](https://www.codacy.com/gh/Hexagon/minitz/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Hexagon/minitz&utm_campaign=Badge_Grade) | ||
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hexagon/minitz/blob/main/LICENSE) | ||
|
||
## Features | ||
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. | ||
|
@@ -16,23 +16,23 @@ | |
* Works in browsers as standalone, UMD or ES-module. | ||
* Includes [TypeScript](https://www.typescriptlang.org/) typings. | ||
|
||
Try it live on [jsfiddle](https://jsfiddle.net/hexag0n/3mheu5by/) | ||
Try Minitz live on [jsfiddle](https://jsfiddle.net/hexag0n/3mheu5by/) | ||
|
||
Check out the full documentation at [minitz.56k.guru](https://minitz.56k.guru) | ||
|
||
## Usage | ||
|
||
Converting a Date object to another timezone in JavaScript is possible using the Intl feature of vanilla JS. | ||
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: | ||
|
||
```javascript | ||
// Get current time in Asia/Tokyo, using vanilla js | ||
new Date().toLocaleString("sv-SE", { timeZone: "Asia/Tokyo" }); | ||
// -> 2022-09-15 17:23:45 | ||
``` | ||
|
||
However - if you want to convert date/time _from_ another timezone, or convert between different timezones, things get trickier. | ||
|
||
Minitz is a minimal library built to solve the problem in the simplest possible way, and made to work in all environments (Node/Deno/Browser, ESM/UMD/CommonJS). | ||
|
||
Simple examples of conversion from a remote timezone, and a conversion between different timezones. | ||
Using minitz to convert **from** and **across** different time zones: | ||
|
||
```javascript | ||
// Get local time from time in Asia/Tokyo, using minitz and vanilla js | ||
|
@@ -49,145 +49,25 @@ console.log( localTime.toLocaleString("sv-SE", { timeZone: "America/New_York" }) | |
// -> 2022-09-15 10:00:00 | ||
``` | ||
|
||
More examples are available further below, and the full documentation is available at [hexagon.github.io/minitz](https://hexagon.github.io/minitz/). | ||
|
||
## Installation | ||
|
||
### Node.js | ||
|
||
```npm install minitz --save``` | ||
|
||
JavaScript | ||
|
||
```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.* | ||
|
||
```typescript | ||
import minitz from "minitz"; | ||
|
||
// ... | ||
``` | ||
Details on installation for various platforms, including Node.js, Deno, Bun, and browsers, can be found in the [full documentation](https://minitz.56k.guru). A few installation commands are: | ||
|
||
### Deno | ||
Node.js: `npm install minitz` | ||
|
||
JavaScript | ||
Deno: `import minitz from "https://deno.land/x/[email protected]/src/minitz.js";` | ||
|
||
```javascript | ||
import minitz from "https://deno.land/x/[email protected]/src/minitz.js"; | ||
|
||
// ... | ||
``` | ||
|
||
TypeScript | ||
|
||
```typescript | ||
import { minitz } from "https://deno.land/x/[email protected]/src/minitz.js"; | ||
|
||
// ... | ||
``` | ||
|
||
Check [https://deno.land/x/minitz](https://deno.land/x/minitz) for latest available version | ||
|
||
### Bun | ||
|
||
```bun add minitz``` | ||
|
||
> **Note** If you experience problems during install, try using `bun add minitz --backend=copyfile`. | ||
JavaScript | ||
|
||
```javascript | ||
import minitz from "minitz"; | ||
``` | ||
Bun: `bun add minitz` | ||
|
||
### Browser | ||
For CDN, refer to the documentation. | ||
|
||
#### Manual | ||
|
||
* Download the latest [zipball](https://github.com/Hexagon/minitz/archive/refs/heads/main.zip) | ||
* Extract | ||
* Grab ```minitz.min.js``` (UMD and standalone) or ```minitz.min.mjs``` (ES-module) from the [dist/](/dist) folder | ||
|
||
#### CDN | ||
|
||
To use as an [UMD](https://github.com/umdjs/umd)-module (stand alone, [RequireJS](https://requirejs.org/) etc.) | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/minitz/dist/minitz.min.js"></script> | ||
``` | ||
|
||
To use as an [ES-module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) | ||
|
||
```html | ||
<script type="module"> | ||
import minitz from "https://cdn.jsdelivr.net/npm/minitz/dist/minitz.min.mjs"; | ||
// ... see usage section ... | ||
</script> | ||
``` | ||
|
||
### More examples | ||
|
||
The examples below will work only if you have imported minitz as described in the 'Installation' section. If that's not the case the results may vary. | ||
|
||
#### Convert a specific timezone to local time | ||
|
||
Standard way | ||
|
||
```javascript | ||
// Convert 2022-09-10 23:08:09 in New York to local time (in this example Europe/Stockholm) | ||
console.log("Local time: ", minitz(2022, 9, 10, 23, 8, 9, "America/New_York").toLocaleString("sv-SE")); | ||
// Local time: 2022-09-11 05:08:09 | ||
``` | ||
|
||
Providing an ISO8601 timestring | ||
|
||
```javascript | ||
// Convert 2022-09-10 23:08:09 in New York to local time (in this example Europe/Stockholm) | ||
console.log("Local time: ", minitz("2022-09-10 23:08:99", "America/New_York").toLocaleString("sv-SE")); | ||
// Local time: 2022-09-11 05:08:09 | ||
``` | ||
|
||
#### Convert local time to a specific timezone | ||
|
||
Provided that you only need to display the result, converting local time to specific timezone is best achieved with vanilla JavaScript. | ||
|
||
```javascript | ||
console.log("Time in New York printed with system locale: ", new Date().toLocaleString("sv-SE", { timeZone: "America/New_York"})); | ||
// -> Time in New York printed with system locale: 2022-09-14 17:29:42 | ||
``` | ||
## Contributing | ||
|
||
If you need to use the result in any other way, it's better to use minitz to convert to a remote timezone. This way you'll get the results as an object, which also includes the timezone to which the time is converted to. | ||
Any contributions are welcome. See the [Contribution guide](https://minitz.56k.guru/contributing.html) to get started. | ||
|
||
```javascript | ||
// Convert to local time to time in America/New_York | ||
// As time in other timezones than local cannot be represented correctly by a date object | ||
// a generic object is returned | ||
console.log("Time in New York: ", minitz.toTZ(new Date(), "America/New_York")); | ||
// -> Time in New York: | ||
// { | ||
// y: 2022, | ||
// m: 9, | ||
// d: 14, | ||
// h: 17, | ||
// i: 29, | ||
// s: 42, | ||
// tz: 'America/New_York' | ||
// } | ||
``` | ||
|
||
## Contributing | ||
## Donations | ||
|
||
Any contributions are welcome. See [Contribution Guide](/CONTRIBUTING.md) | ||
If you found this library helpful and wish to support its development, consider making a donation through [Hexagon's GitHub Sponsors page](https://github.com/sponsors/hexagon). Your generosity ensures the library's continued development and maintenance. | ||
|
||
## License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gem "jekyll-default-layout" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
title: Minitz - The Manual | ||
|
||
description: Minimal, yet powerful libraryto manage and manipulate time zones for native JavaScript date objects. | ||
|
||
remote_theme: just-the-docs/just-the-docs | ||
|
||
url: https://minitz.56k.guru | ||
|
||
aux_links: | ||
GitHub Repository: https://github.com/hexagon/minitz | ||
NPM Mobule: https://npmjs.com/package/minitz | ||
Deno Library: https://deno.land/x/minitz | ||
|
||
plugins: | ||
- jekyll-relative-links | ||
- jekyll-default-layout | ||
|
||
enable_copy_code_button: true | ||
|
||
# Footer content | ||
footer_content: "Copyright © 2021-2023 Hexagon. Distributed by an <a href=\"https://github.com/hexagon/minitz/tree/main/LICENSE\">MIT license.</a>" | ||
last_edit_time_format: "%b %e %Y at %I:%M %p" | ||
gh_edit_link: true | ||
gh_edit_link_text: "Edit this page on GitHub." | ||
gh_edit_repository: "https://github.com/hexagon/minitz" | ||
gh_edit_branch: "main" | ||
gh_edit_source: docs | ||
gh_edit_view_mode: "tree" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9224018205432249" | ||
crossorigin="anonymous"></script> | ||
<!-- Display --> | ||
<ins class="adsbygoogle" | ||
style="display:block" | ||
data-ad-client="ca-pub-9224018205432249" | ||
data-ad-slot="7417217449" | ||
data-ad-format="auto" | ||
data-full-width-responsive="true"></ins> | ||
<script> | ||
(adsbygoogle = window.adsbygoogle || []).push({}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9224018205432249" crossorigin="anonymous"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9224018205432249" | ||
crossorigin="anonymous"></script> | ||
<ins class="adsbygoogle" | ||
style="display:block" | ||
data-ad-format="autorelaxed" | ||
data-ad-client="ca-pub-9224018205432249" | ||
data-ad-slot="5087509523"></ins> | ||
<script> | ||
(adsbygoogle = window.adsbygoogle || []).push({}); | ||
</script> |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
layout: page | ||
title: "Donations" | ||
nav_order: 5 | ||
--- | ||
|
||
## Donations | ||
|
||
If you found this library helpful and wish to support its development, consider making a donation through [Hexagon's GitHub Sponsors page](https://github.com/sponsors/hexagon). Your support helps ensure continued development and maintenance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
layout: page | ||
title: "Examples" | ||
nav_order: 3 | ||
--- | ||
|
||
## Examples | ||
|
||
The examples below will work only if you have imported minitz as described in the 'Installation' section. If that's not the case the results may vary. | ||
|
||
### Convert a specific timezone to local time | ||
|
||
Standard way | ||
|
||
```javascript | ||
// Convert 2022-09-10 23:08:09 in New York to local time (in this example Europe/Stockholm) | ||
console.log("Local time: ", minitz(2022, 9, 10, 23, 8, 9, "America/New_York").toLocaleString("sv-SE")); | ||
// Local time: 2022-09-11 05:08:09 | ||
``` | ||
|
||
Providing an ISO8601 timestring | ||
|
||
```javascript | ||
// Convert 2022-09-10 23:08:09 in New York to local time (in this example Europe/Stockholm) | ||
console.log("Local time: ", minitz("2022-09-10 23:08:99", "America/New_York").toLocaleString("sv-SE")); | ||
// Local time: 2022-09-11 05:08:09 | ||
``` | ||
|
||
### Convert local time to a specific timezone | ||
|
||
Provided that you only need to display the result, converting local time to specific timezone is best achieved with vanilla JavaScript. | ||
|
||
```javascript | ||
console.log("Time in New York printed with system locale: ", new Date().toLocaleString("sv-SE", { timeZone: "America/New_York"})); | ||
// -> Time in New York printed with system locale: 2022-09-14 17:29:42 | ||
``` | ||
|
||
If you need to use the result in any other way, it's better to use minitz to convert to a remote timezone. This way you'll get the results as an object, which also includes the timezone to which the time is converted to. | ||
|
||
```javascript | ||
// Convert to local time to time in America/New_York | ||
// As time in other timezones than local cannot be represented correctly by a date object | ||
// a generic object is returned | ||
console.log("Time in New York: ", minitz.toTZ(new Date(), "America/New_York")); | ||
// -> Time in New York: | ||
// { | ||
// y: 2022, | ||
// m: 9, | ||
// d: 14, | ||
// h: 17, | ||
// i: 29, | ||
// s: 42, | ||
// tz: 'America/New_York' | ||
// } | ||
``` |
Oops, something went wrong.