Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
/ relative_date Public archive

A tiny NPM package which, when given a date, returns a human-friendly relative value from the current date.

License

Notifications You must be signed in to change notification settings

samrith-s/relative_date

Repository files navigation

Relative Date

🚨 This package was an experimental package with absolutely no optimizations to the code or to the build process. It has no purpose any longer. Try out dayjs instead.

Build Status Coverage Status npm GitHub license

A simple package which returns relative human-readable text for any valid date entered. Accepts both JavaScript Date and moment

Installation

To install, simply run

npm install relative_date

Usage

var RelativeDate = require('relative_date');

console.log(RelativeDate(new Date()).text);
// Output: 1:23am

console.log(RelativeDate(moment().subtract(1, 'day')).text);
//Output: Yesterday

console.log(
  RelativeDate(moment().subtract(Math.floor(Math.random() * 6) + 2, 'd')).text
);
//Output: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday

console.log(RelativeDate(moment(new Date()).subtract(8, 'd')).text);
//Output: 21 May, 2017

console.log(RelativeDate(moment().add(100, 'y')).text);
//Output: 100 years from now

console.log(RelativeDate(moment([2017, 40, 20])).text);
//Output: Invalid Date

The value returned by RelativeDate is an object of this order:

RelativeDate(new Date())

// Returned value
{
    value: date.toISOString(),
    text: String,
    valid: Boolean
}

If the date is invalid, the text returned would be "Invalid Date" and the value would be an Invalid Date object.

Tests

You can run tests to check for all the available outcomes, by running yarn test

Issues

All issues and feature requests can be raised in the issues section.

Contributing

ESLint and Prettier are setup to have consistent formatting. Please raise a PR and do not forget to add tests for any cases you might add!

Roadmap

  • Customizable format for case 'Date is today'
  • Customizable format for case 'Date is a parsed date of format D MMM, YYYY'
  • Customizable format for case 'Date is from the future'
  • Additional sub-cases for 'Date is today'
    • Option to show texts like, 'a few seconds ago', '1 minute ago', '7 hours ago' etc.
    • Option to show texts like, 'just now', 'about a minute', etc.

Copyright © 2018.

About

A tiny NPM package which, when given a date, returns a human-friendly relative value from the current date.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published