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 🙏

© 2024 – Pkg Stats / Ryan Hefner

human-friendly-date

v1.0.2

Published

A pretty simple library to format dates and times in human-readable formats.

Downloads

146

Readme

Human Friendly Date 🤗

A simple JavaScript library to format dates in a human-friendly way, returning relative time expressions such as "5 minutes ago" or "in 2 hours". It also provides the formatted date in a specified locale. The library will support multiple languages in future releases.

Installation

npm install human-friendly-date

Usage

The HumanFriendlyDate function provides a convenient way to format timestamps into human-readable strings, including both relative time (e.g., "5 seconds ago", "in 1 hour") and absolute dates (e.g., "Tuesday, November 12, 2024"). It also handles errors gracefully and can be extended for future use cases.

Example 1: Relative Time (Past)

const { HumanFriendlyDate } = require('human-friendly-date');

// Get the current timestamp
const date = new Date();
const timestamp = date.getTime() - 5000; // 5 seconds ago

// Format the timestamp into a human-friendly format
const result = HumanFriendlyDate(timestamp);

console.log(result.timeAgo);     // Output: "5 seconds ago"
console.log(result.timeFromNow); // Output: "in 5 seconds"

Example 2: Relative Time (Future)

const { HumanFriendlyDate } = require('human-friendly-date');

// Get the current timestamp and add 1 hour
const date = new Date();
const timestamp = date.getTime() + 3600000; // 1 hour from now

// Format the future timestamp
const result = HumanFriendlyDate(timestamp);

console.log(result.timeAgo);     // Output: "1 hour ago"
console.log(result.timeFromNow); // Output: "in 1 hour"

Formatting Absolute Dates

Use the formatDate function to format a timestamp into a human-readable date in the format: Day of the week, Month Day, Year.

const { HumanFriendlyDate } = require('human-friendly-date');

// Get the current timestamp
const date = new Date();
const timestamp = date.getTime();

// Format the timestamp into a full date string
const result = HumanFriendlyDate(timestamp);

console.log(result.formattedDate); // Output: "Tuesday, November 12, 2024"

Handling Invalid Timestamps

If you pass an invalid timestamp, the function will throw an error:

try {
  HumanFriendlyDate('invalid', 'en');
} catch (error) {
  console.error(error.message); // Output: "Invalid timestamp"
}

Methods

  • HumanFriendlyDate(timestamp) Returns an object with relative time and formatted date.

  • HumanFriendlyDate(timestamp, languageCode) (Optional) Specify a language code (e.g., 'en' for English) for localization.

  • timestamp: A valid timestamp (in milliseconds). languageCode: The language code for formatting (e.g., 'en' for English).

Returns

The function returns an object with the following properties:

  • timeAgo: A string representing the relative time (e.g., "5 seconds ago").
  • timeFromNow: A string representing the time from now (e.g., "in 5 seconds").
  • formattedDate: A string representing the fully formatted date (e.g., "Tuesday, November 12, 2024").

License

MIT