0.2.0 • Published 8 years ago

readable-timestamp v0.2.0

Weekly downloads
35
License
MIT
Repository
github
Last release
8 years ago

readable-timestamp

Generates a human readable timestamp that tells how many time has elapsed since a given date until now. If more than 30 days have elapsed, generates a short date string: day + abbreviated month + year (last one only if the given date it's not in the current year).

Elapsed timeOutput example
Less than a minuteJust now
One minuteA minute ago
Between 1 - 60 minutes7 minutes ago
One hourAn hour ago
Between 1 - 24 hours13 hours ago
One dayA day ago
Between 1 - 30 days4 days ago
More than a month, but in the current year23 Feb
More than a month, but in another year9 Dec 2015

Usage

Works both required as CommonsJS module in node or in the browser.

As a CommonsJS module it exports a function, and in a browser environment declares 'readableTime()' function in the global scope.

var readableTime = require('readable-timestamp');

var now = new Date();

// It will log 'Just now'.
console.log(readableTime(now));

You can also generate absolute timestamps providing an options.format as the second parameter. Accepts 'absolute', 'absolute-full' and 'absolute-short'.

var readableTime = require('readable-timestamp');

// At time of writing this, it was 15 April 2016.
var now = new Date();

// It will log '15 Apr', but if 'now' contained a date from 15 April 2015,
// it would log '15 Apr 2015', because it's form the past year. Using the
// 'absolute-full' format will always add the year and with 'absolute-short'
// it won't never do it.
console.log(readableTime(now, { format: 'absolute' }));