node package manager

elapsed

A module for getting the elapsed time between two dates in various representation.

npm install elapsed

A module for getting the elapsed time between two dates in various representation.

var Elapsed = require('elapsed');
 
var elapsedTime = new Elapsed(new Date(2013, 05, 2), new Date(2013, 05, 25));
 
console.log(elapsedTime.minutes.num); // 33120 
console.log(elapsedTime.hours.text);  // "552 hours" 
console.log(elapsedTime.optimal);     // "3 weeks" 
var elapsedTime = new Elapsed(from, to, i10n);

The from (required) and to (optional, default: now) must be Date objects. i10n (optional: default: english) must be a plain object.

  • milliSeconds: (Object), num property is the time in Number, text is the time in String.
  • seconds: (Object), num property is the time in Number, text is the time in String.
  • minutes: (Object), num property is the time in Number, text is the time in String.
  • hours: (Object), num property is the time in Number, text is the time in String.
  • days: (Object), num property is the time in Number, text is the time in String.
  • weeks: (Object), num property is the time in Number, text is the time in String.
  • months: (Object), num property is the time in Number, text is the time in String.
  • years: (Object), num property is the time in Number, text is the time in String.
  • optimal: (String), the best from the ones above.
  • from: (Date)
  • to: (Date)
  • set(): calculating the properties.
  • refresh(to): refresh the to date. to is optional it defaults to now.

If you want to localize the elapsed time, you can provide an object holding the translations by passing it into the constructor as a third argument.

var i10n = {};
var elapsedTime = new Elapsed(from, to, i10n);

Take a look at the english version, to get an impression of how the object should look like:

var i10n = {
  milliSeconds: ['millisecond', 's'],
seconds: ['second', 's'],
minutes: ['minute', 's'],
hours: ['hour', 's'],
days: ['day', 's'],
weeks: ['week', 's'],
months: ['month', 's'],
years: ['year', 's']
};

Each property holds an array containing the singular and the plural-suffix.

If you don't need to specify the to-parameter, you can pass in the localization as second parameter:

var i10n = {};
var elapsedTime = new Elapsed(from, i10n);