prettify-date

1.4.0 • Public • Published

prettify-date

Convert a javascript Date object to a human readable string.

npm package npm downloads

If in last 1 minute return just now.

If in last 1 hour return number of minutes e.g. 10 min.

If in last 24 hrs return number of hours 4 hrs.

Otherwise return in format: Month Day at Time AM/PM

e.g. November 15th at 10:35 AM.

Note: date must be a date object (new Date()).

Install

npm install --save prettify-date

Use

const prettifyDate = require('prettify-date');
 
const secToMs = ms => 1000 * ms;
const minToMs = m => 60 * secToMs(m);
const hrToMs = hr => 60 * minToMs(hr);
const dayToMs = d => 24 * hrToMs(d);
const secAgo = s => new Date(new Date() - secToMs(s));
const minAgo = m => new Date(new Date() - minToMs(m));
const hrAgo = hr => new Date(new Date() - hrToMs(hr));
const dayAgo = d => new Date(new Date() - dayToMs(d));
 
console.log(prettifyDate.format(secAgo(0)));
console.log(prettifyDate.format(secAgo(1)));
console.log(prettifyDate.format(secAgo(59)));
console.log(prettifyDate.format(minAgo(1)));
console.log(prettifyDate.format(minAgo(59)));
console.log(prettifyDate.format(hrAgo(1)));
console.log(prettifyDate.format(hrAgo(23)));
console.log(prettifyDate.format(dayAgo(1)));
console.log(prettifyDate.format(dayAgo(2)));
console.log(prettifyDate.format(dayAgo(10)));
console.log(prettifyDate.format(dayAgo(100)));

Example Output:

  • just now
  • just now
  • just now
  • 1 min
  • 59 mins
  • 1 hr
  • 23 hrs
  • yesterday
  • December 1 at 2:02 AM
  • November 23 at 2:02 AM
  • August 25 at 3:02 AM

Package Sidebar

Install

npm i prettify-date

Weekly Downloads

1

Version

1.4.0

License

ISC

Unpacked Size

3.7 kB

Total Files

3

Last publish

Collaborators

  • peterdulworth