Nodejs Number to Readable Format toHumanTime(elapsed)

Here you can find the source of toHumanTime(elapsed)

Method Source Code

/**/*  w ww  .  j av  a 2  s  .c om*/
 * Returns a human readable timespan.
*/
Number.toHumanTime = function(elapsed) {
  var labels = ['ms', 's', 'm', 'h', 'd'];
  var sizes = [1000, 60, 60, 24];
  var data = [];
  sizes.forEach(function(value) {
    data.push(elapsed % value);
    elapsed = parseInt(elapsed / value);
  });
  var pos = 0;
  data.forEach(function(value, index) {
    if (value > 0) {
      pos = index;
    }
  });
  var result = data[pos];
  if (pos > 0) {
    result += '.' + parseInt(data[pos - 1] / sizes[pos - 1] * 10);
  }
  result += labels[pos];
  return result;
};

Related

  1. toHumanSize(bytes)
    Number.toHumanSize = function(bytes) {
      var labels = ['Bytes', 'kB', 'MB', 'GB', 'TB'];
      if (bytes === 0) {
        return 'n/a';
      var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
      return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + labels[i];
    };
    
  2. humanReadableOrder()
    Number.prototype.humanReadableOrder = function() {
      if (this>=1000000000) {
        return (this/1000000000).toFixed(1)+'b';
      if (this>=1000000) {
        return (this/1000000).toFixed(1)+'m';
      if (this>=1000) {
        return (this/1000).toFixed(1)+'k';
    ...
    
  3. humanReadablePrice()
    Number.prototype.humanReadablePrice = function() {
      return (this/10000).toFixed(2);
    };
    
  4. toEnglish()
    var numbersToWords = {
      0: 'zero',
      1: 'one',
      2: 'two',
      3: 'three',
      4: 'four',
      5: 'five',
      6: 'six',
      7: 'seven',
    ...
    
  5. toEnglish()
    var numbersToWords = {
      0: 'zero',
      1: 'one',
      2: 'two',
      3: 'three',
      4: 'four',
      5: 'five',
      6: 'six',
      7: 'seven',
    ...