# readable-elapsed-timer

> Easily get time elapsed. Returns human readable format by default.

Latest version **0.4.3** (published 2024-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install readable-elapsed-timer
pnpm add readable-elapsed-timer
yarn add readable-elapsed-timer
bun add readable-elapsed-timer
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.3 |
| Published | 2024-10-30 |
| First published | 2019-12-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Michael Charles Aubrey |
| Maintainers | mca62511 |
| Keywords | readable, time, elapsed, timer |

## Links

- npm: https://www.npmjs.com/package/readable-elapsed-timer
- Repository: https://github.com/mcaubrey/readable-elapsed-timer
- Homepage: https://github.com/mcaubrey/readable-elapsed-timer#readme
- Issues: https://github.com/mcaubrey/readable-elapsed-timer/issues
- npm.io page: https://npm.io/package/readable-elapsed-timer

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 0.4.3 (latest) — 2024-10-30
- 0.4.2 — 2024-10-30
- 0.4.1 — 2024-10-30
- 0.4.0 — 2024-10-30
- 0.3.1 — 2020-07-27
- 0.3.0 — 2020-01-02
- 0.2.2 — 2019-12-04
- 0.2.1 — 2019-12-03
- 0.2.0 — 2019-12-03
- 0.1.2 — 2019-12-03
- 0.1.1 — 2019-12-03
- 0.1.0 — 2019-12-03

## README

![](./coverage/badge-lines.svg) ![](./coverage/badge-functions.svg) ![](./coverage/badge-branches.svg) ![](./coverage/badge-statements.svg)

# Readable Elapsed Timer

A tool for easily finding the time elapsed between two parts of your code.

## Usage

Import the `Timer` class at the top of your file. If your project uses ES6 modules then do the following.

```
import { Timer } from "readable-elapsed-timer";
```

If your poject uses CommonJS modules, then do the following.

```
const { Timer } = require('readable-elapsed-timer')
```

Then you can create a new timer object to keep track of time within your code. Call the `elapsed()` method to return the time elapsed in a human readable format. To get the time between two different points without creating a new timer object, simply call the `reset()` method.

```
// Delay function for demonstration purposes.
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

const main = async () => {
  const timer = new Timer();
  await delay(100);
  console.log(timer.elapsed());
  // "100 milliseconds"

  timer.reset()
  await delay(500);
  console.log(timer.elapsed());
  // "500 milliseconds"

  await delay(1000);
  console.log(timer.elapsed());
  // "1.5 seconds"
  // Notice we didn't reset the timer this time.
};

main()
```

## Install

```
npm install readable-elapsed-time
```

## Advanced Usage

The constructor accepts an `options` object as an argument.

```
const options = {
    start: new Date('November 5, 1955').getTime(),
    brief: true,
    language: 'ja'  // Supports 'en' (default) or 'ja' for Japanese output
}
```

All of the `elapsed` family of methods accept an `options` argument as well. In addition to the options available on the constructor, you can set an `end` time. This allows you to get time elapsed without actually needing to wait.

```
const options = {
    start: new Date("November 5, 1955").getTime(),
    end: new Date("October 21, 2015").getTime(),
    brief: false
};

const elapsed = timer.elapsed(options);

console.log(elapsed);
// "525600 hours"
```

Setting `start` allows you to give the timer instance an arbitrary start time. Setting `brief` changes the default function of the timer instance to show time formatted as abbreviations ("5s" instead of "5 seconds"). Setting `language` to "ja" will output times in Japanese format (e.g., "5秒" instead of "5 seconds").

The following additional methods are available on an instance of `Timer`.

- `elapsedVerbose()` This will return the time elapsed formatted to be human readable even if the timer is configured otherwise. If `timer.elapsedVerbose()` is called after five seconds, `5 seconds` will be returned.
- `elapsedBrief()` This will return the time elapsed formatted using abbreviations even if the timer is configured otherwise. If `timer.elapsedBrief()` is called after five seconds, `5s` will be returned.
- `elapsedRaw()` This will return the time elapsed in milliseconds without any additional formatting. If `timer.elapsedRaw()` is called after five seconds, `5000` will be returned.
- `elapsedRaw()` This will return the time elapsed in milliseconds without any additional formatting. If `timer.elapsedRaw()` is called after five seconds, `5000` will be returned.

# Releases

* 0.3.1 - Added two more tests for the undocumented language feature. Updated dependencies.
* 0.4.3 - Fixed issues with type declarations. You should be able to more easily import this library now.

---
_Source: https://npm.io/package/readable-elapsed-timer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
