EvanHahn / HumanizeDuration.js

361000 becomes "6 minutes, 1 second"
https://evanhahn.github.io/HumanizeDuration.js/
The Unlicense
1.65k stars 175 forks source link

Add option for abbreviated units #103

Closed jsejcksn closed 8 years ago

jsejcksn commented 8 years ago

This library is so excellent! After seeing that Moment.js did not support granular display, I was very glad to see this.

I am wondering if it wouldn't be too difficult to implement abbreviated units of time. For example:

(I'm sure there's a standard for this somewhere).

The reason why I'm asking is because I'm using this library for sending push notifications and it would save a lot of space if abbreviations could be used.

EvanHahn commented 8 years ago

You can do this by creating a humanizer with a new language, like so:

var shortEnglishHumanizer = humanizeDuration.humanizer({
  language: 'shortEn',
  languages: {
    shortEn: {
      y: function() { return 'y' },
      mo: function() { return 'mo' },
      w: function() { return 'w' },
      d: function() { return 'd' },
      h: function() { return 'h' },
      m: function() { return 'm' },
      s: function() { return 's' },
      ms: function() { return 'ms' },
    }
  }
})

shortEnglishHumanizer(15600000)  // '4 h, 20 m'

Does that help?

jsejcksn commented 8 years ago

@EvanHahn Definitely! Sorry, I clearly missed that part of the documentation when I skimmed it. Should have read more thoroughly before posting. Thanks.

EvanHahn commented 8 years ago

No worries!