Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vague-time

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vague-time

A tiny library that formats precise time differences as a vague/fuzzy time.

  • 2.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
105
decreased by-52.91%
Maintainers
1
Weekly downloads
 
Created
Source

vagueTime.js

A tiny JavaScript library that formats precise time differences as a vague/fuzzy time.

Build status

Why would I want that?

Displaying precise dates and times can make a website feel stuffy and formal. Using vague or fuzzy time phrases like "just now" or "3 days ago" can contribute to a friendlier interface.

vagueTime.js provides a small, clean API for converting timestamps into user-friendly phrases, heavily supported by unit tests.

How tiny is it?

3.7 kb unminified with comments, 1.2 kb minified or 693 bytes minified+gzipped.

What doesn't it do?

Older versions of this library used to include translations into languages other than English. That translation process was both imperfect and a source of complexity, whereas the raison d'être of this library was to be small and simple. Localisation is a separate problem, better addressed by a dedicated solution. So, in an effort to do one thing well, the translation code was removed. It is still available in the 1.x branch and, of course, you are welcome to fork this repo if you preferred things how they were.

This library only converts in one direction: from dates/timestamps to strings. If you're interested in the opposite transformation, look elsewhere.

What alternative libraries are there?

How do I install it?

Via npm:

npm i vague-time

Or if you just want the git repo:

git clone git@github.com:philbooth/vagueTime.js.git

How do I use it?

Loading the library

If you are running in Node.js, Browserify or another CommonJS-style environment, you can require vagueTime.js like so:

var vagueTime = require('vague-time');

It also the supports the AMD-style format preferred by Require.js.

If you are including vagueTime.js with an HTML <script> tag, or neither of the above environments are detected, the interface will be globally available as vagueTime.

Calling the exported functions

vagueTime.js exports a single public function, get, which returns a vague time string based on the argument(s) that you pass it.

The arguments are passed as properties on a single options object:

  • from: Timestamp or Date instance denoting the origin point from which the vague time will be calculated. Defaults to Date.now().
  • to: Timestamp or Date instance denoting the target point to which the vague time will be calculated. Defaults to Date.now().
  • units: String denoting the units that the from and to timestamps are specified in. May be 's' for seconds or 'ms' for milliseconds. Defaults to 'ms'. This property has no effect when from and to are Date instances rather than timestamps.

Essentially, if to is less than from, the returned vague time will indicate some point in the past. If to is greater than from, it will indicate some point in the future.

Examples

const vagueTime = require('vague-time');

vagueTime.get({
  from: 60,
  to: 0,
  units: 's'
}); // returns 'a minute ago'

vagueTime.get({
  from: 0,
  to: 3600000,
  units: 'ms'
}); // returns 'in an hour'

vagueTime.get({
  from: new Date(2017, 0, 31),
  to: new Date(2016, 10, 30)
}); // returns '2 months ago'

How do I set up the dev environment?

Install the dependencies:

npm i

Lint the code:

npm run lint

Run the tests:

npm test

Or, to run the tests in a web browser, open test/vagueTime.html.

What changed between version 1.x and 2.x?

Support for languages other than English was removed in release 2.0.0. If you were relying on that stuff, I'm sorry. You may be interested in the 1.x branch.

What license is it released under?

MIT

Keywords

FAQs

Package last updated on 06 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc