Skip to content

bolshakov/Humanizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Humanizer

Humanizer is a library, which knows how to measure approximate distance between the two given dates. For example, the difference between now and now - 25 seconds ago is less than a minute -- obvious, right?

Examples

First, we'll need a bunch of helpers -- m() for minutes and s() for seconds:

s = (sec) -> sec * 1000
m = (min) -> min * s(60)

Humanizer.between(from, to, includeSeconds)

Returns the approximate distance in time between two given objects, which can either be Date or Number instances. When includeSeconds is true, Humanizer calculates a more precise approximation, for intervals under one and a half minutes.

coffee> from = new Date()
Sat, 23 Jul 2011 00:52:59 GMT
coffee> Humanizer.between(from, from - m(65))
"about 1 hour"
coffee> Humanizer.between(from, from + s(15), true)
"less than 20 seconds"

Humanizer.since(from, to, includeSeconds)

Just like #between(), but the second argument is fixed to new Date().

coffee> Humanizer.since(new Date() + m(3))
"3 minutes"
coffee> Humanizer.since(new Date())
"less than a minute"

Usage

Add humanizer.js to your <head /> and don't forget to include the locales you need:

<head>
  <script src="lib/humanizer.js"></script>
  <script src="lib/locales/humanizer.fr.js"></script>
  <script src="lib/locales/humanizer.en.js"></script>
  <script>
    var bicycleDay = Date.parse("November 16, 1938");
    Humanizer.locale("fr").since(bicycleDay);
  </script>
</head>

Building the library

Easy as pie! erm cake:

$ cake build

This will compile humanizer.coffee and also fetch all available locales from Padrino, translating them into a format Humanizer can understand.

Copyright

Source code was extracted from Padrino project and translated into CoffeeScript. See LICENSE file for details.

About

Approximate distance in time between two dates.

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors