Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit ad190a5

Browse files
committed
Add display format option for duration + add phantomJS to dev-dep
1 parent 7798910 commit ad190a5

5 files changed

Lines changed: 30 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Note: To use `amFromUnix`, install angular-moment version 1.0.0-beta.3 or newer
112112

113113
### amUtc filter
114114

115-
Create / switch the current moment object into UTC mode. For example, given a date object in `message.date`,
115+
Create / switch the current moment object into UTC mode. For example, given a date object in `message.date`,
116116
the following code will display the time in UTC instead of the local timezone:
117117

118118
```html
@@ -134,7 +134,7 @@ Note: To use `amUtcOffset`, install angular-moment version 1.0.0-beta.3 or newer
134134

135135
### amLocal filter
136136

137-
Changes the given moment object to be in the local timezone. Usually used in conjunction with `amUtc` / `amTimezone`
137+
Changes the given moment object to be in the local timezone. Usually used in conjunction with `amUtc` / `amTimezone`
138138
for timezone conversion. For example, the following will convert the given UTC date to local time:
139139

140140
```html
@@ -195,8 +195,8 @@ For more information about Moment.JS difference function, see the
195195

196196
### amDurationFormat filter
197197

198-
Formats a duration (such as 5 days) in a human readable format. See [Moment.JS documentation](http://momentjs.com/docs/#/durations/creating/)
199-
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
198+
Formats a duration (such as 5 days) in a human readable format. If a display format is provided (as third argument), duration is formatted according to this argument instead of being humanized. See [Moment.JS documentation](http://momentjs.com/docs/#/durations/creating/)
199+
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
200200
for explanation about the formatting algorithm.
201201

202202
Example:
@@ -207,6 +207,11 @@ Example:
207207

208208
Will display the age of the message (e.g. 10 minutes, 1 hour, 2 days, etc).
209209

210+
```html
211+
<span>Next train in {{train.nextDuration | amDurationFormat : 'seconds':undefined:'minutes' }} minutes</span>
212+
213+
Will display "Next train in 3 minutes" if train.nextDuration is 190.
214+
210215
### amSubtract filter
211216

212217
Subtract values (hours, minutes, seconds ...) from a specified date.

angular-moment.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,21 @@
593593
* @function
594594
*/
595595
.filter('amDurationFormat', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) {
596-
function amDurationFormatFilter(value, format, suffix) {
596+
function amDurationFormatFilter(value, format, suffix, displayFormat) {
597+
var units = ['milliseconds', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'];
598+
var asUnits = units.map(function (unit) {
599+
return 'as' + unit.charAt(0).toUpperCase() + unit.slice(1);
600+
});
601+
var validDisplayFormats = units.concat(asUnits);
602+
597603
if (isUndefinedOrNull(value)) {
598604
return '';
599605
}
606+
if (isUndefinedOrNull(displayFormat) || validDisplayFormats.indexOf(displayFormat) < 0) {
607+
return moment.duration(value, format).humanize(suffix);
608+
}
600609

601-
return moment.duration(value, format).humanize(suffix);
610+
return moment.duration(value, format)[displayFormat]();
602611
}
603612

604613
amDurationFormatFilter.$stateful = angularMomentConfig.statefulFilters;

angular-moment.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)