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

Commit ac02a28

Browse files
committed
Add display format option for duration + add phantomJS to dev-dep
1 parent 943238d commit ac02a28

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

README.md

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

9292
### amUtc filter
9393

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

9797
```html
@@ -113,7 +113,7 @@ Note: To use `amUtcOffset`, install angular-moment version 1.0.0-beta.3
113113

114114
### amLocal filter
115115

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

119119
```html
@@ -174,8 +174,8 @@ For more information about Moment.JS difference function, see the
174174

175175
### amDurationFormat filter
176176

177-
Formats a duration (such as 5 days) in a human readable format. See [Moment.JS documentation](http://momentjs.com/docs/#/durations/creating/)
178-
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
177+
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/)
178+
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
179179
for explanation about the formatting algorithm.
180180

181181
Example:
@@ -186,6 +186,11 @@ Example:
186186

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

189+
```html
190+
<span>Next train in {{train.nextDuration | amDurationFormat : 'seconds':undefined:'minutes' }} minutes</span>
191+
192+
Will display "Next train in 3 minutes" if train.nextDuration is 190.
193+
189194
### amSubtract filter
190195

191196
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
@@ -584,12 +584,21 @@
584584
* @function
585585
*/
586586
.filter('amDurationFormat', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) {
587-
function amDurationFormatFilter(value, format, suffix) {
587+
function amDurationFormatFilter(value, format, suffix, displayFormat) {
588+
var units = ['milliseconds', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'];
589+
var asUnits = units.map(function (unit) {
590+
return 'as' + unit.charAt(0).toUpperCase() + unit.slice(1);
591+
});
592+
var validDisplayFormats = units.concat(asUnits);
593+
588594
if (isUndefinedOrNull(value)) {
589595
return '';
590596
}
597+
if (isUndefinedOrNull(displayFormat) || validDisplayFormats.indexOf(displayFormat) < 0) {
598+
return moment.duration(value, format).humanize(suffix);
599+
}
591600

592-
return moment.duration(value, format).humanize(suffix);
601+
return moment.duration(value, format)[displayFormat]();
593602
}
594603

595604
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)