JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32671
  • Score
    100M100P100Q135284F
  • License MIT

An extremely lightweight javascript utility for calculating readable time differences from now vs past or future dates.

Package Exports

  • fromnow

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fromnow) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

FromNow

An extremely lightweight javascript utility for calculating readable time differences from now vs past or future dates.

2 kb uncompressed. <1 kb minified (before gzip)

fromNow(date [, options]);

fromNow.js only has 1 required parameter, a date string. You may pass it any valid datestring.

Installation

Install with Bower bower install fromnow

Install with NPM npm install fromnow

Install with Git git clone https://github.com/lukeed/fromnow.git

Options

maxChunks
Type: Integer. Optional. Default: all
Limits the returned string to contain # of not-null segments. Gathers largest to smallest.
Eg: 1 month, 0 hours, 57 minutes limited to 2 chunks returns 1 month, 57 minutes

useAgo
Type: Boolean. Optional. Default: false
If given date occured before current datetime, append 'ago' to returned string.
Eg: 3 months, 16 minutes vs 3 months, 16 minutes ago

useAnd
Type: Boolean. Optional. Default: false
If true, will add ' and ' between penultimate and ultimate chunks.
Eg: 1 year, 4 hours, 16 minutes vs 1 year, 4 hours, and 16 minutes
Eg: 2 days, 12 hours vs 2 days and 12 hours

Usage

Default

fromNow.js assumes that you only want the time difference (string), so it will yield all values.

fromNow('12/31/2010'); // "4 years, 10 months, 8 days, 10 hours, 15 minutes"
fromNow('2030-05-20'); // "14 years, 6 months, 21 days, 5 hours, 43 minutes"
fromNow('2030-05-20 14:02:47'); // "14 years, 6 months, 22 days, 2 hours, 44 minutes"
fromNow('Wed, 20 Nov 1912 00:00:00 GMT'); // "103 years, 23 days, 18 hours, 20 minutes"

Limit the Output

fromNow('12/31/2010', {
  maxChunks: 3
}); // "4 years, 10 months, 8 days"

fromNow('2030-05-20', {
  maxChunks: 2
}); // "14 years, 6 months"

Indicate Past Tense

fromNow('12/31/2010', {
    maxChunks: 3,
    useAgo: true
}); // "4 years, 10 months, 8 days ago"

Include 'and' in the Output

fromNow('12/31/2010', {
    maxChunks: 3,
    useAgo: true,
    useAnd: true
}); // "4 years, 10 months, and 8 days ago"

fromNow('Wed, 20 Nov 1912 00:00:00 GMT', {
  maxChunks: 2,
  useAgo: true,
  useAnd: true
}); // "103 years and 23 days ago"

// Does not need 'ago' or 'and'
fromNow('2030-05-20', {
  maxChunks: 1,
  useAgo: true,
  useAnd: true
}); // "14 years"

MIT license

fromNow.js is released under the MIT license.