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

relative-time-converter

v1.0.0

Published

relative-time-converter is a lightweight utility that converts absolute dates or timestamps into human-friendly, relative time phrases like "2 hours ago" or "in 3 days." Perfect for improving date displays in apps, websites, or dashboards.

Downloads

59

Readme

Relative Time Converter

relative-time-converter is a lightweight utility that converts absolute dates or timestamps into human-friendly, relative time phrases like "2 hours ago" or "in 3 days." Perfect for improving date displays in apps, websites, or dashboards.

Features

  • Convert absolute dates into relative time phrases.
  • Get the difference between two dates in years, months, days, hours, minutes, and seconds.
  • Display relative time strings for past and future dates.
  • Error handling for invalid date objects.

Installation

You can install the package using npm:

npm install relative-time-converter

Usage

Here's a basic example of how to use the Relative Time Converter:

import { RelativeTimeConverter } from 'relative-time-converter';

const converter = new RelativeTimeConverter();

try {
    const pastDate = new Date('2023-10-01T10:00:00');
    const futureDate = new Date('2025-10-01T10:00:00');
    const invalidDate = 'invalid date string';

    const pastResult = converter.convertToRelativeTime(pastDate);
    console.log(pastResult);
    // Sample output: 
    // {
    //   relativeTime: "1 year 1 month 3 days ago",
    //   years: 1,
    //   months: 1,
    //   days: 3,
    //   hours: 0,
    //   minutes: 0,
    //   seconds: 0
    // }

    const futureResult = converter.convertToRelativeTime(futureDate);
    console.log(futureResult);
    // Sample output: 
    // {
    //   relativeTime: "in 1 year 1 month",
    //   years: 1,
    //   months: 1,
    //   days: 0,
    //   hours: 0,
    //   minutes: 0,
    //   seconds: 0
    // }
} catch (error) {
    console.error(error.message);
}

Method

convertToRelativeTime(date: Date): RelativeTimeResult

Converts a Date object into a relative time object.

Parameters:

  • date: A Date object to be converted.

Returns:

  • A JSON object containing the relative time string and the difference between the input date and the current date in years, months, days, hours, minutes, and seconds or throws an error if the input is not a valid Date object.

Example Responses

Example For a Past Date:

{
    "relativeTime": "1 year 3 months 5 days ago",
        "years": 1,
        "months": 3,
        "days": 5,
        "hours": 0,
        "minutes": 0,
        "seconds": 0
}

Example For a Future Date:

{
  "relativeTime": "in 2 years 5 months 3 days",
  "years": 2,
  "months": 5,
  "days": 3,
  "hours": 0,
  "minutes": 0,
  "seconds": 0
}

Example For Now:

{
  "relativeTime": "Just Now!",
  "years": 0,
  "months": 0,
  "days": 0,
  "hours": 0,
  "minutes": 0,
  "seconds": 0
}

Error Handling

If the input is not a valid Date object, the method will throw an error.

try {
    converter.convertToRelativeTime("invalid date string");
} catch (error) {
    console.error(error.message); // Output: "Invalid date object!"
}

Setup on Your Local Machine

To set up the project on your local machine, follow these steps:

Clone the repository:

git clone [email protected]:evrentan/relative-time-converter.git

Navigate to the project directory:

cd relative-time-converter

Install the dependencies:

npm install

Build the project:

npm run build

Test the project:

npm test

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/yourFeature).
  3. Commit your changes (git commit -m 'feat(Feature): Add some Feature').
  4. Push to the branch (git push origin feature/yourFeature).
  5. Open a pull request.

License

This project is licensed under the ISC License.

About

This library was created to help developers display human-friendly relative time phrases in their applications. If you have any questions or suggestions, feel free to reach out!

Sponsoring

If you enjoy this project and would like to support its development, please consider sponsoring. Your support helps me continue improving and maintaining the project.

You can sponsor me via:

GitHub Sponsors

Buy Me a Coffee

Thank you for your support!