JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17
  • Score
    100M100P100Q49047F
  • License MIT

A simple and lightweight Node.js library for human-friendly time formatting and localization.

Package Exports

  • humanize-time-tool
  • humanize-time-tool/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (humanize-time-tool) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

# humanize-time-tool

![npm version](https://img.shields.io/npm/v/humanize-time-tool.svg)  
![npm downloads](https://img.shields.io/npm/dm/humanize-time-tool.svg)  
![license](https://img.shields.io/npm/l/humanize-time-tool.svg?cacheBust=1)  

A simple and lightweight Node.js library for human-friendly time formatting with built-in localization (i18n) support.

## Features

- Format timestamps as "time ago" strings (e.g., "5 minutes ago")
- Supports 20+ languages with easy language switching
- Customizable and extendable translations  
- Lightweight and dependency-free  
- Automatic fallback to Swedish if translation is missing  
- Suitable for server-side rendering and client-side use

## Supported Languages

<!-- LANGUAGES_START -->
- ar - Arabic  
- cs - Czech  
- da - Danish  
- de - German  
- el - Greek  
- en - English  
- es - Spanish  
- fi - Finnish  
- fr - French  
- he - Hebrew  
- hi - Hindi  
- it - Italian  
- ja - Japanese  
- ko - Korean  
- nl - Dutch  
- no - Norwegian  
- pl - Polish  
- pt - Portuguese  
- ru - Russian  
- sv - Swedish  
- tr - Turkish  
- vi - Vietnamese  
- zh - Chinese  
<!-- LANGUAGES_END -->

## Installation

```bash
npm install humanize-time-tool

Usage

import { timeAgo } from 'humanize-time-tool';

console.log(timeAgo(new Date(Date.now() - 5 * 60 * 1000), 'en')); // "5 minutes ago"
console.log(timeAgo('2025-01-01T00:00:00Z', 'sv'));              // "för några sekunder sedan" (exempel på svensk översättning)

console.log(timeAgo(Date.now() - 3600 * 1000, 'en', { hour_one: 'an hour ago' })); // Custom override for one-hour string

Important Notes

  • Translations are fully included inside the JavaScript code and loaded automatically.
  • There is no need to load external translation files or call loadTranslations() in normal usage.
  • The loadTranslations() function remains exported only for advanced use cases like dynamically updating or overriding translations at runtime.

API Reference

timeAgo(dateInput, lang = 'sv', customTranslations = {})

Formats a given date into a localized "time ago" string.

  • dateInput: Date object, ISO string, or timestamp
  • lang: Language code (default 'sv')
  • customTranslations: Optional object to override specific translation strings

Returns a string such as "5 minutes ago" or "för 5 minuter sedan".

getTranslations(lang)

Returns the translations object for the specified language if available; otherwise returns undefined.

loadTranslations()

(Optional) Asynchronously loads translations from an external JSON file and merges them with built-in defaults. Normally not needed since translations are included in the package.

License

MIT