0.7.0 • Published 7 years ago

datum.js v0.7.0

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

datum.js

datum.js is a javascript utility that makes it easy to print readable dates, e.g. 4 minutes ago.

Install the package from npm

npm install --save datum.js

Usage

Just pass a Date object to the function and datum.js will convert it to a readable string.

Datum.js supports the following epochs

  • year
  • month
  • day
  • hour
  • minute
import datum from 'datum.js';

// Create a new date that reflects 2 minutes ago
const oneMinutesAgo = new Date().setMinutes(new Date().getMinutes() - 1);
// Create a new date that reflects 5 days ago
const oneDaysAgo = new Date().setDate(new Date().getDate() - 1);

datum(oneMinutesAgo); // => '1 minute ago'
datum(oneDaysAgo); // => '1 day ago'

If the returned value is greater then one, we will add a suffix to the epoch

import datum from 'datum.js';

// Create a new date that reflects 2 minutes ago
const twoMinutesAgo = new Date().setMinutes(new Date().getMinutes() - 2);
// Create a new date that reflects 5 days ago
const twoDaysAgo = new Date().setDate(new Date().getDate() - 2);

datum(twoMinutesAgo); // => '2 minutes ago'
datum(twoDaysAgo); // => '2 days ago'

API

datum(date, [options])

The parse method takes a Date object and returns a string

date

The date param always need to be a date object and will always return a string.

Options

datum.js allows you to change the exception value, i.e the value that will be displayed when the date is less than 1 minute to anything you want.

var options = {
    exception: '0 minutes a go',
};

License

MIT. Copyright (c) 2017 Philip Knape.