Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

angular-moment

urish281.4kMIT1.3.0

angular-moment ==============

readme

angular-moment

AngularJS directive and filters for Moment.JS.

Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018 Uri Shaked uri@urish.org

Build Status Coverage Status

New Angular version is now available: ngx-moment


Installation

You can choose your preferred method of installation:

  • Through bower: bower install angular-moment --save
  • Through npm: npm install angular-moment moment --save
  • Through NuGet: Install-Package angular-moment Moment.js
  • From a CDN: jsDelivr or CDNJS
  • Download from github: angular-moment.min.js

Instructions for using moment-timezone with webpack

Even if you have moment-timezone in your package.json, angular-moment will not be able to use it unless you override moment using Angular's dependency injection See Resolved Issue

 var angular = require('angular');
 require('angular-moment');
 var ngModule = angular.module('ngApp',['angularMoment']);
 ngModule.constant('moment', require('moment-timezone'));

Usage

Include both moment.js and angular-moment.js in your application.

<script src="components/moment/moment.js"></script>
<script src="components/angular-moment/angular-moment.js"></script>

Add the module angularMoment as a dependency to your app module:

var myapp = angular.module('myapp', ['angularMoment']);

If you need internationalization support, load specified moment.js locale file right after moment.js:

<script src="components/moment/moment.js"></script>
<script src="components/moment/locale/de.js"></script>
<script src="components/angular-moment/angular-moment.js"></script>

Then call the amMoment.changeLocale() method (e.g. inside your app's run() callback):

myapp.run(function(amMoment) {
    amMoment.changeLocale('de');
});

Use in controller/service/factory

Inject the moment-constant into your dependency injection. For example:

angular.module('fooApp')
    .controller('FooCtrl', ['$scope', 'moment', function ($scope, moment) {
        $scope.exampleDate = moment().hour(8).minute(0).second(0).toDate();
    }]);

am-time-ago directive

Use the am-time-ago directive to format your relative timestamps. For example:

<span am-time-ago="message.time"></span>

angular-moment will dynamically update the span to indicate how much time passed since the message was created. So, if your controller contains the following code:

$scope.message = {
   text: 'hello world!',
   time: new Date()
};

The user will initially see "a few seconds ago", and about a minute after the span will automatically update with the text "a minute ago", etc.

amParse filter

Parses a custom-formatted date into a moment object that can be used with the am-time-ago directive and the other filters. For example, the following code will accept dates that are formatted like "2015.04.25 22:00:15':

<span am-time-ago="message.time | amParse:'YYYY.MM.DD HH:mm:ss'"></span>

amFromUnix filter

Converts a unix-timestamp (seconds since 1/1/1970) into a moment object. Example:

<span am-time-ago="message.unixTime | amFromUnix">

amUtc filter

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

<span>{{message.date | amUtc | amDateFormat:'MM.DD.YYYY HH:mm:ss'}}</span>

amUtcOffset filter

Uses the given utc offset when displaying a date. For example, the following code will display the date with a UTC + 3 hours time offset:

<span>{{message.date | amUtcOffset:'+0300' | amDateFormat:'MM.DD.YYYY HH:mm:ss'}}</span>

amLocal filter

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

<span>{{message.date | amUtc | amLocal | amDateFormat:'MM.DD.YYYY HH:mm:ss'}}</span>

amTimezone filter

Applies a timezone to the given date / moment object. You need to include moment-timezone.js in your project and load timezone data in order to use this filter. The following example displays the time in Israel's timezone:

<span>{{message.date | amTimezone:'Israel' | amDateFormat:'MM.DD.YYYY HH:mm:ss'}}</span>

amDateFormat filter

Format dates using moment.js format() method. Example:

<span>{{message.time | amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}}</span>

This snippet will format the given time as "Monday, October 7th 2013, 12:36:29 am".

For more information about Moment.JS formatting options, see the docs for the format() function.

amCalendar filter

Format dates using moment.js calendar() method. Example:

<span>{{message.time | amCalendar:referenceTime:formats}}</span>

This snippet will format the given time as e.g. "Today 2:30 AM" or "Last Monday 2:30 AM" etc..

For more information about Moment.JS calendar time format, see the docs for the calendar() function.

amDifference filter

Get the difference between two dates in milliseconds. Parameters are date, units and usePrecision. Date defaults to current date. Example:

<span>Scheduled {{message.createdAt | amDifference : null : 'days' }} days from now</span>

This snippet will return the number of days between the current date and the date filtered.

For more information about Moment.JS difference function, see the docs for the diff() function.

amDurationFormat filter

Formats a duration (such as 5 days) in a human readable format. See Moment.JS documentation for a list of supported duration formats, and humanize() documentation for explanation about the formatting algorithm.

Example:

<span>Message age: {{message.ageInMinutes | amDurationFormat : 'minute' }}</span>

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

amSubtract filter

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

See Moment.JS documentation for a list of supported duration formats.

Example:

<span>Start time: {{day.start | amSubtract : '1' : 'hours' | amDateFormat : 'hh'}} : {{day.start | amSubtract : '30' : 'minutes' | amDateFormat : 'mm'}}</span>

amAdd filter

Add values (hours, minutes, seconds ...) to a specified date.

See Moment.JS documentation for a list of supported duration formats.

Example:

<span>Start time: {{day.start | amAdd : '1' : 'hours' | amDateFormat : 'hh'}} : {{day.start | amAdd : '30' : 'minutes' | amDateFormat : 'mm'}}</span>

amStartOf filter

Mutates the original moment by setting it to the start of a unit(minute, hour, day..) of time.

See Moment.JS documentation for a list of supported duration formats.

Example:

<span>{{ date | amStartOf:'month' | amLocal }}</span>

amEndOf filter

Mutates the original moment by setting it to the end of a unit(minute, hour, day..) of time.

See Moment.JS documentation for a list of supported duration formats.

Example:

<span>{{ date | amEndOf:'month' | amLocal }}</span>

Time zone support

The amDateFormat and amCalendar filters can be configured to display dates aligned to a specific timezone. You can configure the timezone using the following syntax:

angular.module('myapp').constant('angularMomentConfig', {
    timezone: 'Name of Timezone' // e.g. 'Europe/London'
});

Remember to include moment-timezone.js v0.3.0 or greater in your project, otherwise the custom timezone functionality will not be available. You will also need to include a timezone data file that you can create using the Timezone Data Builder or simply download from here.

Accessing moment() in your JavaScript

If you wish to use moment() in your services, controllers, or directives, simply inject the moment variable into the constructor.

License

Released under the terms of the MIT License.

changelog

Changelog

1.3.0 - 2018-09-04

  • Update moment.js version in bower.json (#306, contributed by jniles)

1.2.0 - 2017-12-02

  • Update moment.js version in bower.json (#300, contributed by jc1arke)

1.1.0 - 2017-09-28

  • Add additional typeof check for the global.moment instance (#295, contributed by schester44)

1.0.1 - 2016-12-01

1.0.0 - 2016-10-06

  • Improvments to the UMD code

Breaking change: The AMD version now exports the module name ('angularMoment') instead of the actual module object.

1.0.0-beta.6 - 2016-04-24

  • Support for setting the units of the full date threshold of am-time-ago (#237, contributed by denistrustepain)
  • Add optional arguments referenceTime and formats to the amCalendar filter (#241, contributed by Nitro-N)
  • Support moment 2.13.x and above

1.0.0-beta.5 - 2016-03-18

  • Bugfix: amTimeAgo shouldn't convert the time to local timezone on the element's title attribute (#226, contributed by stackia)
  • Support moment 2.12.x

1.0.0-beta.4 - 2016-02-09

  • Add amStartOf and amEndOf filter (#203, contributed by pratik14)
  • Support Moment 2.11.x
  • Happy Year of the Monkey!

1.0.0-beta.3 - 2015-11-10

  • Support AngularJS 1.5.x
  • Support for nw.js (#196, contributed by makkesk8)
  • Bugfix: title attribute does update when model changes (#201, contributed by stackia)

1.0.0-beta.2 - 2015-09-20

  • Bugfix: Infinite digest loop when combining am-time-ago and amTimezone (#178)
  • Bugfix: Cannot use angular-moment under webpack (#108)
  • Add amLocal filter (see #114)

1.0.0-beta.1 - 2015-09-14

!!! BREAKING CHANGE !!!

Preprocessors, timezones and input format were removed from am-time-ago and all filters. Use the new amFromUnix, amUtc, amUtcOffset, amTimezone, and amParse filters instead.

Examples:

  • <time am-time-ago="myDate" am-format="YYYY-MM-DD"> becomes <time am-time-ago="myDate|amParse:'YYYY-MM-DD'">
  • <time am-time-ago="myDate" am-preprocess="unix"> becomes <time am-time-ago="myDate|amFromUnix">
  • {{myDate|amCalendar:'unix'}} becomes {{myDate|amFromUnix|amCalendar}}
  • {{myDate|amCalendar:null:'PDT'}} becomes {{myDate|amTimezone:'PDT'|amCalendar}}

The removal of the preprocessors also affects the other positional parameters of the amTimeAgo:

{{myDate|amTimeAgo:null:true:fromDate}} becomes {{myDate|amTimeAgo:true:fromDate}}.

For more information, please see #174.

0.10.3 - 2015-09-05

  • Allow amDateFormat to work with custom formatted input date strings (#162, contributed by jblashka)
  • amAdd, amSubtract - add/subtract a value from a given date (#171, contributed by nicholasruggeri)
  • Bugfix: Timezones with a 'Z' somewhere in them all become UTC (#168).

0.10.2 - 2015-07-28

  • Look for moment on the global object (#133, contributed by kitbrennan90)
  • Add support to use UTC offset timezones in addition to named timezones (#151, contributed by DiegoZoracKy)
  • Add timezone parameter for amCalendar filter (#152, contributed by DiegoZoracKy)
  • Add am-from parameter to the amTimeAgo directive (#145, contributed by baleato)
  • Add from parameter to the amTimeAgo filter (#146, contributed by pipo02mix)

0.10.1 - 2015-05-01

  • Fix broken SystemJS/JSPM support (see #104)

0.10.0 - 2015-04-10

  • Breaking change: removed one-time binding for am-time-ago in favor of AngularJS 1.3's one time binding (#122)
  • Remove support for AngularJS 1.0.x and 1.1.x.
  • Support moment.js v2.10.x
  • Support for displaying full dates in am-time-ago (see #75)
  • Support Angular Core's style CommonJS standard (#123, contributed by seanhealy)
  • Added an optional timezone parameter to amDateFormat (#90, contributed by robertbrooker)

0.9.2 - 2015-03-17

  • Critical fix: npm install angular-moment fails (#121)

0.9.1 - 2015-03-17

  • Add support for locale strings customization (#102, contributed by vosi)
  • Add amDifference filter (#120, contributed by ajhodges)
  • Support for changing the timezone via amMoment.changeTimezone() (#92)
  • Support for AngularJS 1.4.x
  • Remove explicit module name for RequireJS (#112, contributed by WilliamCarter)

0.9.0 - 2015-01-11

  • Support moment.js v2.9.0. See here for changelog.
  • Removed support for older moment.js versions. Only 2.8.0 and newer versions are now supported.
  • Removed deprecated method: amMoment.changeLanguage(). Use amMoment.changeLocale() instead.
  • Removed deprecated event: amMoment:languageChange. Listen for amMoment:localeChange instead.
  • Filters are now stateful by default (fixes #97).
  • The project is now available on NuGet (#99, contributed by markvp).

0.8.3 - 2014-12-08

  • amTimeAgo filter (#96, contributed by maxklenk)
  • Show formatted time as element title (#78, contributed by ctesene)
  • Support commonjs and browserify (#95, contributed by Pencroff)
  • SystemJS Loader support (#85, contributed by capaj)

0.8.2 - 2014-09-07

  • amMoment.changeLanguage() was deprecated in favor of amMoment.changeLocale() (following a change introduced in moment v2.8.1)
  • Bugfix: changing the locale emitted a deprecation warning (see #76 for details).

0.8.1 - 2014-09-01

  • Support moment.js v2.8.0. See here for changelog.
  • Support moment-timezone v0.2.1. See here for changelog.
  • Bugfix: updateTime() is called too often for future dates (#73)

0.8.0 - 2014-07-26

  • Generate source map for the minified version (#50)
  • Add support HTML <time> element - set the datetime attribute (#41, contributed by gsklee)
  • Add default format (angularMomentConfig.format config property) (#52, contributed by otang)
  • Add serverTime configuration option (#53, contributed by Facundo Pedrazzini)
  • Implement one-time binding for am-time-ago (#54, contributed by Ephi Gabay)
  • Support moment.js v2.7.0. See here for changelog.
  • Support moment-timezone v0.1.0. See here for changelog.

0.7.1 - 2014-05-16

  • bugfix: Preprocess set in configuration not used by filters (#49)

0.7.0 - 2014-04-19

  • Use moment as an injectable constant instead of relying on $window.moment (#35, contributed by just-boris)
  • Require.js support (#36)
  • Add am-preprocess attribute to support unix and utc timestamps (#38, contributed by jspaper)
  • NGDoc documentation (#40)
  • Enable support for AngularJS 1.3.x in bower.json
  • Support moment.js v2.6.0. See here for changelog.

0.6.2 - 2014-02-05

  • Add amMoment service with a changeLanguage() method (#32, contributed by Ornthalas)
  • bower.json: Move moment-timezone to devDependencies (fixes #34)

0.6.1 - 2014-01-31

  • Add optional timezone support to amCalendar and amDateFormat filters (#27, contributed by kayhadrin)
  • Happy Year of the Horse!

0.6.0 - 2013-12-24

  • Add optional am-without-suffix attribute to am-time-ago (#22, contributed by hramaker)
  • Support moment.js v2.5.0. See here for changelog.
  • Merry Christmas!

0.5.2 - 2013-11-17

  • Add amCalendar filter (#24, contributed by OndraM)

0.5.1 - 2013-11-09

0.5.0 - 2013-11-02

  • Use $window.setTimeout instead of $timeout, fixes protractor synchronization issue (#19)

0.4.2 - 2013-10-30

  • Add settings constant for configuring moment.js withoutSuffix-option (#18)

0.4.1 - 2013-10-27

  • Support moment.js v2.4.0. See here for changelog.

0.4.0 - 2013-10-08

  • Support moment.js v2.3.0. See here for possibly breaking changes.

0.3.0 - 2013-10-07

  • Bugfix: am-time-ago support for empty string (#15)
  • Behavior change: am-time-ago will only change the text once there is date

0.2.2 - 2013-09-29

  • Add support for passing unix timestamp as a string to amDateFormat filter (#14)

0.2.1 - 2013-09-13

  • Fix an issue with tests failing on a different timezone
  • Support moment 2.2.x, AngularJS 1.2

0.2.0 - 2013-08-22

  • Add optional am-format attribute to am-time-ago (#11)
  • Add new amDateFormat filter (#12)
  • Add changelog file

0.1.1 - 2013-06-08

0.1.0 - 2013-05-27

  • Initial release