Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document passing thresholds to humanize #630

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions docs/moment/08-durations/03-humanize.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ title: Humanize
version: 1.6.0
signature: |
moment.duration().humanize();
moment.duration().humanize(withSuffix);
moment.duration().humanize(withSuffix, thresholds); // from 2.25.0
moment.duration().humanize(thresholds); // from 2.25.0
---


Sometimes, you want all the goodness of `moment#from` but you don't want to have to create two moments, you just want to display a length of time.
Sometimes, you want all the goodness of `moment#from` but you don't want to
have to create two moments, you just want to display a length of time.

Enter `moment.duration().humanize()`.

Expand All @@ -16,7 +20,9 @@ moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize(); // a day
```

By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
By default, the return string is describing a duration `a month` (suffix-less).
If you want an oriented duration `in a month`, `a month ago` (with suffix),
pass in true as seen below.

```javascript
moment.duration(1, "minutes").humanize(true); // in a minute
Expand All @@ -33,3 +39,14 @@ Invalid durations are humanized to the localized version of `Invalid Date`.
```javascript
moment.duration.invalid().humanize(); // Invalid Date
```

Humanize output can be configured with relative time thresholds. To specify
thresholds for a particular invocation of humanize, pass them as a sole
argument or after suffix arg:

```javascript
moment.duration(-1, 'week').humanize(true, {d: 7, w: 4}); // a week ago
moment.duration(-1, 'week').humanize({d: 7, w: 4}); // a week
```

**Note**: Passing thresholds in humanize was added in **2.25.0**.