Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/risadams/time-since
A JavaScript function to calculate the elapsed time between a specified value and now
https://github.com/risadams/time-since
date elapsed-time hacktoberfest javascript time utility
Last synced: 25 days ago
JSON representation
A JavaScript function to calculate the elapsed time between a specified value and now
- Host: GitHub
- URL: https://github.com/risadams/time-since
- Owner: risadams
- License: mit
- Created: 2022-05-16T10:19:47.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-06T11:55:04.000Z (about 1 month ago)
- Last Synced: 2024-10-06T11:59:22.025Z (about 1 month ago)
- Topics: date, elapsed-time, hacktoberfest, javascript, time, utility
- Language: JavaScript
- Homepage:
- Size: 1.43 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
- Support: SUPPORT.md
Awesome Lists containing this project
README
# Time-Since
This project provides an enhanced JavaScript utility function `timeSince` that calculates the time elapsed since a given date, with support for various time units and human-readable formats in different locales. The `timeSince` function is useful for applications that need to display time differences in a flexible and customizable manner.
## Features
- Calculates time elapsed in various units such as years, months, days, hours, minutes, seconds, and milliseconds.
- Supports customizable output formats (e.g., `relative`, `milliseconds`, `days`, etc.).
- Utilizes `Intl.RelativeTimeFormat` to provide human-readable relative time in different locales.
- Handles both string and `Date` object inputs.## Install
```sh
npm install @risadams/time-since
```## Usage
The `timeSince` function can be used to calculate the elapsed time from a given date and return the result in different formats. Below are some examples of how to use the function.
### Example 1: Basic Usage
```javascript
const timeSince = require('@risadams/time-since');const elapsedTime = timeSince('2020-01-01');
console.log(elapsedTime);
// Output: { years: 4, months: 3, days: ..., hours: ..., minutes: ..., seconds: ..., milliseconds: ... }
```### Example 2: Custom Format
```javascript
const elapsedDays = timeSince('2020-01-01', { format: 'days' });
console.log(elapsedDays);
// Output: 1461 (assuming the current year is 2024)
```### Example 3: Relative Time in Different Locales
```javascript
const relativeTimeFr = timeSince('2020-01-01', { format: 'relative', locale: 'fr' });
console.log(relativeTimeFr);
// Output: "il y a 4 ans"
```## Running Tests
The project includes unit tests written using the Jest framework. These tests cover all major functionalities of the `timeSince` function, including various input formats and customizable options.
To run the tests:
```bash
npm test
```The test suite includes:
- Handling of both string and `Date` object inputs.
- Validation of different time units (e.g., `milliseconds`, `seconds`, `minutes`, etc.).
- Testing of relative time outputs in different locales.## Contribute
If you think this could be better, please [open an issue](https://github.com/risadams/time-since/issues/new)!
Please note that all interactions in this organization fall under our [Code of Conduct](CODE_OF_CONDUCT.md).
## License
[MIT](LICENSE) © 1996+ Ris Adams