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

Commit fe563f4

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

6 files changed

+34
-11
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Note: To use `amFromUnix`, install angular-moment version 1.0.0-beta.2
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.2
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

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function isUndefinedOrNull(val) {
1010
return angular.isUndefined(val) || val === null;
1111
}
12-
12+
1313
function requireMoment() {
1414
try {
1515
return require('moment'); // Using nw.js or browserify?
@@ -19,7 +19,7 @@
1919
}
2020

2121
function angularMoment(angular, moment) {
22-
22+
2323
if(typeof moment === 'undefined') {
2424
if(typeof require === 'function') {
2525
moment = requireMoment();
@@ -583,12 +583,21 @@
583583
* @function
584584
*/
585585
.filter('amDurationFormat', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) {
586-
function amDurationFormatFilter(value, format, suffix) {
586+
function amDurationFormatFilter(value, format, suffix, displayFormat) {
587+
var units = ['milliseconds', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'];
588+
var asUnits = units.map(function (unit) {
589+
return 'as' + unit.charAt(0).toUpperCase() + unit.slice(1);
590+
});
591+
var validDisplayFormats = units.concat(asUnits);
592+
587593
if (isUndefinedOrNull(value)) {
588594
return '';
589595
}
596+
if (isUndefinedOrNull(displayFormat) || validDisplayFormats.indexOf(displayFormat) < 0) {
597+
return moment.duration(value, format).humanize(suffix);
598+
}
590599

591-
return moment.duration(value, format).humanize(suffix);
600+
return moment.duration(value, format)[displayFormat]();
592601
}
593602

594603
amDurationFormatFilter.$stateful = angularMomentConfig.statefulFilters;

angular-moment.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)