FromNow 
A tiny (339B) utility for human-readable time differences between now and past or future dates.
Install
$ npm install fromnow --save
Usage
A valid date string is the only required parameter.
const fromNow = require('fromnow');
fromNow('12/31/2010');
fromNow('2030-05-20');
fromNow('2030-05-20 14:02:47');
fromNow('Wed, 20 Nov 1912 00:00:00 GMT');
API
fromNow(date, options={})
Returns: String
A valid date string is the only required parameter.
date
Type: String
You may pass it any valid date string.
options.max
Type: Integer
Default: null
If set, will limits the return to display a maximum number of non-null segments.
Important: When opts.zero = true
then empty segments will count towards your max
limit!
"2 years, 0 months, 12 hours, 57 minutes"
"2 years, 0 months"
"2 years, 12 hours, 57 minutes"
"2 years, 12 hours"
options.suffix
Type: Boolean
Default: false
Appends "ago"
or "from now
" to the output.
fromNow("Sun Jun 14 2015 14:09:05", { and:true, suffix:true });
fromNow("Sun Jun 14 2017 14:09:05", { and:true, suffix:true, max:2 });
options.and
Type: Boolean
Default: false
Join the last two segments with " and "
.
"1 year, 4 hours, 16 minutes"
"2 days, 12 hours"
options.zero
Type: Boolean
Default: false
Return segments with 0
value.
fromNow("Sun Jun 14 2015 15:14:05");
fromNow("Sun Jun 14 2015 15:14:05", { zero:true });
Examples
Limit the Output
fromNow('12/31/2010', { max:3 });
fromNow('2030-05-20', { max:2 });
Indicate Past or Future Tense
fromNow('12/31/2010', { max:3, suffix:true });
fromNow('12/31/2030', { max:1, suffix:true });
Include 'and' in the Output
fromNow('12/31/2010', { max:3, suffix:true, and:true });
fromNow('Wed, 20 Nov 1912 00:00:00 GMT', { max:2, suffix:true, and:true });
fromNow('2030-05-20', { max:1, and:true });
License
MIT © Luke Edwards