# @meecrobe/react-time-hook

> 🕑 React hook for formatting string to 12- or 24-hour time.

Latest version **0.5.0** (published 2021-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @meecrobe/react-time-hook
pnpm add @meecrobe/react-time-hook
yarn add @meecrobe/react-time-hook
bun add @meecrobe/react-time-hook
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2021-11-26 |
| First published | 2021-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12.14.0 |
| Dependencies | 0 |
| Unpacked size | 55.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vadzim Tsupryk |
| Maintainers | meecrobe |
| Keywords | react, hooks, time, typescript, react-hooks, custom-hooks |

## Links

- npm: https://www.npmjs.com/package/@meecrobe/react-time-hook
- Repository: https://github.com/meecrobe/react-time-hook
- Homepage: https://meecrobe.github.io/react-time-hook/
- Issues: https://github.com/meecrobe/react-time-hook/issues
- npm.io page: https://npm.io/package/@meecrobe/react-time-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2021-11-26
- 0.4.0 — 2021-11-26
- 0.3.0 — 2021-09-08
- 0.2.1 — 2021-09-08
- 0.2.0 — 2021-09-08
- 0.1.2 — 2021-09-06
- 0.1.1 — 2021-08-10
- 0.1.0 — 2021-08-09

## README

# @meecrobe/react-time-hook

🕑 React hook for formatting string to 12- or 24-hour time.

![Build](https://img.shields.io/github/workflow/status/meecrobe/react-time-hook/CI)

![Demo](https://user-images.githubusercontent.com/8003440/128711176-2d323c95-f5f4-47de-9e9c-d572e01f9e10.gif)

[Live demo](https://meecrobe.github.io/react-time-hook/)

## Installation

```bash
$ yarn add @meecrobe/react-time-hook
```

## Usage

```typescript
import { useCallback, useState } from 'react';
import { useTime } from '@meecrobe/react-time-hook';

function TimeInput() {
  const [value, setValue] = useState('');
  const time = useTime(value);

  const handleChange = React.useCallback((e) => {
    setValue(e.target.value);
  }, []);

  const handleBlur = React.useCallback(() => {
    setValue(time);
  }, [time]);

  return (
    <input
      type="text"
      value={value}
      onChange={handleChange}
      onBlur={handleBlur}
    />
  );
}
```

## Props

| Prop         | Type           | Description                                 |
| ------------ | -------------- | ------------------------------------------- |
| initialValue | `string`       | Initial value                               |
| format       | `'12' \| '24'` | Time format                                 |
| padZero      | `boolean`      | Add leading zero to hours in 12-hour format |

## Examples of string values and their formatting results

<details>
<summary>To 12-hour time format (without leading zeros)</summary>

| Input      | Output        |
| ---------- | ------------- |
| `''`       | `'12:00 AM'`  |
| `'0'`      | `'12:00 AM'`  |
| `'1'`      | `'1:00 AM'`   |
| `'10'`     | `'10:00 AM'`  |
| `'12'`     | `'12:00 PM'`  |
| `'13'`     | `'1:00 PM'`   |
| `'24'`     | `'12:00 AM' ` |
| `'99'`     | `'12:00 AM' ` |
| `'345'`    | `'3:45 AM'`   |
| `'1334' `  | `'1:34 PM' `  |
| `'5889'`   | `'12:00 AM'`  |
| `'1:4'`    | `'1:04 AM'`   |
| `'11:4'`   | `'11:04 AM'`  |
| `'26:09'`  | `'12:09 AM'`  |
| `'12:20'`  | `'12:20 PM' ` |
| `'2000'`   | `'8:00 PM'`   |
| `'3p'`     | `'3:00 PM `   |
| `'8:21p'`  | `'8:21 PM'`   |
| `'821p'`   | `'8:21 PM' `  |
| `'1201am'` | `'12:01 AM'`  |
| `'3:'`     | `'3:00 AM'`   |

</details>

<details>
<summary>To 24-hour time format</summary>

| Input        | Output    |
| ------------ | --------- |
| `'1'`        | `'01:00'` |
| `'10'`       | `'10:00'` |
| `'111'`      | `'01:11'` |
| `'153'`      | `'01:53'` |
| `'1820'`     | `'18:20'` |
| `'2400'`     | `'00:00'` |
| `'1:4'`      | `'01:04'` |
| `'11:4'`     | `'11:04'` |
| `'2244'`     | `'22:44'` |
| `'3p'`       | `'15:00'` |
| `'8:21am'`   | `'08:21'` |
| `'8:21p'`    | `'20:21'` |
| `'821p'`     | `'20:21'` |
| `'3p'`       | `'15:00'` |
| `'8:21p'`    | `'20:21'` |
| `'03:55 AM'` | `'03:55'` |
| `'03:55 PM'` | `'15:55'` |
| `'12:01 AM'` | `'00:01'` |
| `'1201a'`    | `'00:01'` |
| `'00:55'`    | `'00:55'` |
| `'66:66'`    | `'00:00'` |
| `'22:'`      | `'22:00'` |

</details>

---
_Source: https://npm.io/package/@meecrobe/react-time-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
