npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

locale-time-diff

v1.1.1

Published

Display time differences with localization language

Readme

Locale Time Diff

npm-version license typescript

A lightweight and customizable utility to format time differences into phrases with built-in support for multiple languages and easy extensibility.

Features

  • Human-Readable Output: Converts raw time differences into easy-to-understand strings.
  • Localization Support: Comes with English (en) and Vietnamese (vi) locales out-of-the-box.
  • Customizable Locales: Easily add new languages or override existing localization strings.
  • Future and Past Time: Handles both past and future time differences.
  • Flexible Input: Accepts Date objects, Unix timestamps, or date strings.
  • Custom Comparison Time: Allows comparison against a specified time instead of always now.

Installation

npm install locale-time-diff

Usage

Basic usage

Import the getTimeDifference function:

import getTimeDifference from 'locale-time-diff';

const now = new Date();
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000); // 1 hour ago
const twoDaysLater = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000); // 2 days from now

console.log(getTimeDifference(oneHourAgo).text);
// Output (approx.): "1 hour ago"

console.log(getTimeDifference(twoDaysLater).text);
// Output (approx.): "In 2 days"

const justNow = new Date(now.getTime() - 3000); // 3 seconds ago
console.log(getTimeDifference(justNow, { locale: 'en' }).text);
// Output: "Just now"

Custom Comparison Time

You can specify a compareTime instead of using the current time: code

import getTimeDifference from 'locale-time-diff';

const eventTime = new Date('2023-01-15T10:00:00Z');
const customComparisonPoint = new Date('2023-01-15T10:30:00Z'); // 30 minutes after eventTime

const result = getTimeDifference(eventTime, {
  locale: 'en',
  compareTime: customComparisonPoint
});

console.log(result.text);
// Output: "30 minutes ago"

Adding or Overriding Locales

Use the addLocale function to extend defaultLocalization with new languages or customize existing ones.

import { addLocale, getTimeDifference } from 'locale-time-diff';

// Add a new French locale
addLocale('fr', {
  justNow: "À l'instant",
  pastTimeLocalization: {
    second: '{c} seconde auparavant',
    seconds: '{c} secondes auparavant',
    minute: '{c} minute auparavant',
    minutes: '{c} minutes auparavant',
    // ... other time units, inheriting from 'en' if not specified
  },
  futureTimeLocalization: {
    second: 'Dans {c} seconde',
    seconds: 'Dans {c} secondes',
    minute: 'Dans {c} minute',
    minutes: 'Dans {c} minutes',
    // ...
  }
});

const now = new Date();
const tenMinutesAgo = new Date(now.getTime() - 10 * 60 * 1000);

console.log(getTimeDifference(tenMinutesAgo, { locale: 'fr' }).text);
// Output (approx.): "10 minutes auparavant"

// Override a specific string in an existing locale (e.g., Vietnamese)
addLocale('vi', {
  justNow: 'Vừa mới đây!' // Custom "just now" for Vietnamese
});

console.log(getTimeDifference(now, { locale: 'vi' }).text);
// Output: "Vừa mới đây!"

Full Result Object

The getTimeDifference function returns a comprehensive Result object:

import getTimeDifference from 'locale-time-diff';

const now = new Date();
const tenMinutesAgo = new Date(now.getTime() - 10 * 60 * 1000 - 15000); // 10 minutes 15 seconds ago

const result = getTimeDifference(tenMinutesAgo, { locale: 'en' });

console.log(result);
/*
Output (approx.):
{
  text: '10 minutes ago',
  unit: 'minute',
  rawDiffMiliseconds: 615000, // Difference in milliseconds
  diffMiliseconds: 615000,   // Absolute difference
  isFuture: false
}
*/

API

howLongAgo(dateInput: Date | number | string, option: Option): Result

Calculates the time difference and returns a localized string. dateInput: The target time.

  • option: An object configuring the calculation:
  • locale?: string | Localization: A locale key ('en', 'vi', etc.) or a custom Localization object. compareTime?: Date | number: The reference time for comparison. Defaults to new Date().
  • Returns: A Result object.

addLocale(name: string, localize: PartialLocalization): void

Adds a new localization or updates an existing one in defaultLocalization.

  • name: The key for the locale (e.g., 'fr').
  • localize: An object containing localization strings. Unspecified fields will be inherited from the base locale.

License

This project is licensed under the MIT License