Git Product home page Git Product logo

relative_date's Introduction

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.

relative_date's People

Contributors

samrith-s avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.