angular-moment-duration-format
v0.2.1
Published
Fiters for use and format Moment.js duration in AngularJS views
Downloads
239
Readme
Angular moment duration format
Fiters for use and format Moment.js duration in AngularJS views
AngularJS filters that lets you use Moment.js duration in your angular views.
Brings moment-duration-format to angular view.
Heavily inpired by angular-moment.
Installation
Node.js
npm install angular-moment-duration-format
Browser
<script src="path/to/angular.js"></script>
<script src="path/to/moment.js"></script>
<script src="path/to/moment-duration-format.js"></script>
<script src="path/to/angular-moment-duration-format.js"></script>
angular-moment-duration-format
requires angular.js
, moment.js
and moment-duration-format.js
Moment is planning to add duration format in its core (see 1048).
Usage
Prerequisite
Add the module angularDurationFormat
as a dependency to your app module:
var myapp = angular.module('myapp', ['angularDurationFormat']);
General
Each filter can be used on expression accepted by moment.duration
single argument constructor.
Valid inputs:
Number
length of time in millisecondsObject
withseconds
,minutes
,hours
,days
,weeks
,months
,years
keys (or short counterpart:y
,M
,w
,d
,h
,m
,s
,ms
) like{minutes: 2, hours: 2}
String
hour, minute, second string separated by colons like23:59:59
or ISO 8601 duration like'P1Y2M3DT4H5M6S'
To use moment.duration(Number, String)
in your view use amdCreate
filter, passing the unit as argument.
You can find more detailed information about moment.duration
in the official documentation
amdFormat
filter
Formats a duration using moment-duration-format
format
method.
The filter accepts the same parameters of format
method:
template
precision
settings
amdHumanize
filter
Formats a duration using momentjs humanize()
method. See here more info about how moment humanizes duration.
Note that moment documentation has a section about how to customize relative time that be be use to customize output of humanize()
and amdHumanize
filter.
amdCreate
filter
Creates moment.duration
specifying unit. Accepts a unit
parameter.
Returns ISO string representation of duration, in order to be used together with other filters like amdFormat
and amdHumanize
Example:
<span>{{ 15 | amdCreate:'minutes' | amdFormat:'HH:mm' }}</span>
<span>{{ 10 | amdCreate:'hours' | amdHumanize }}</span>
amdAdd
filter
Add value to duration. Wraps moment duration add
.
Returns ISO string representation of duration.
Example:
<span>{{ myDuration | amdAdd:10:'minutes' | amdFormat:'HH:mm' }}</span>
<span>{{ 10 | amdCreate:'hours' | amdAdd:myOtherDuration | amdFormat:'HH:mm' }}</span>
amdSubtract
filter
Subtract value to duration. Wraps moment duration subtract
.
Returns ISO string representation of duration.
Example:
<span>{{ myDuration | amdSubtract:10:'minutes' | amdFormat:'HH:mm' }}</span>
<span>{{ 10 | amdCreate:'hours' | amdSubtract:myOtherDuration | amdFormat:'HH:mm' }}</span>
License
Released under the terms of the MIT License.