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

Package detail

ngx-moment

urish297.6kMIT6.0.2TypeScript support: included

Moment.JS pipes for Angular (timeago and more)

angular, timeago, momentjs

readme

ngx-moment

moment.js pipes for Angular

Build Status npm version

This module works with Angular 7.0.0 and newer.

For older Angular versions, please install angular2-moment. For the AngularJS, please check out angular-moment.

Installation

npm install --save moment ngx-moment

or if you use yarn:

yarn add moment ngx-moment

Usage

Import MomentModule into your app's modules:

import { MomentModule } from 'ngx-moment';

@NgModule({
  imports: [
    MomentModule
  ]
})

If you would like to supply any NgxMomentOptions that will be made available to the pipes you can also use:

import { MomentModule } from 'ngx-moment';

@NgModule({
  imports: [
    MomentModule.forRoot({
      relativeTimeThresholdOptions: {
        'm': 59
      }
    })
  ]
})

This makes all the ngx-moment pipes available for use in your app components.

Available pipes

amTimeAgo pipe

Takes an optional omitSuffix argument that defaults to false and another optional formatFn function which can be used to customise the format of the time ago text.

@Component({
  selector: 'app',
  template: `
    Last updated: {{myDate | amTimeAgo}}
  `
})

Prints Last updated: a few seconds ago

@Component({
  selector: 'app',
  template: `
    Last updated: {{myDate | amTimeAgo:true}}
  `
})

Prints Last updated: a few seconds

amCalendar pipe

Takes optional referenceTime argument (defaults to now) and formats argument that could be output formats object or callback function. See momentjs docs for details.

@Component({
  selector: 'app',
  template: `
    Last updated: {{myDate | amCalendar}}
  `
})

Prints Last updated: Today at 14:00 (default referenceTime is today by default)

@Component({
  selector: 'app',
  template: `
    Last updated: <time>{{myDate | amCalendar:nextDay }}</time>
  `
})
export class AppComponent {
  nextDay: Date;

  constructor() {
      this.nextDay = new Date();
      nextDay.setDate(nextDay.getDate() + 1);
  }
}

Prints Last updated: Yesterday at 14:00 (referenceTime is tomorrow)

@Component({
  selector: 'app',
  template: `
    Last updated: <time>{{myDate | amCalendar:{sameDay:'[Same Day at] h:mm A'} }}</time>
  `
})

Prints Last updated: Same Day at 2:00 PM

amDateFormat pipe

@Component({
  selector: 'app',
  template: `
    Last updated: {{myDate | amDateFormat:'LL'}}
  `
})

Prints Last updated: January 24, 2016

amParse pipe

Parses a custom-formatted date into a moment object that can be used with the other pipes.

@Component({
  selector: 'app',
  template: `
    Last updated: {{'24/01/2014' | amParse:'DD/MM/YYYY' | amDateFormat:'LL'}}
  `
})

Prints Last updated: January 24, 2016

The pipe can also accept an array of formats as parameter.

@Component({
  selector: 'app',
  template: `
    Last updated: {{'24/01/2014 22:00' | amParse: formats | amDateFormat:'LL'}}
  `
})
export class App {

  formats: string[] = ['DD/MM/YYYY HH:mm:ss', 'DD/MM/YYYY HH:mm'];

  constructor() { }

}

Prints Last updated: January 24, 2016

amLocal pipe

Converts UTC time to local time.

@Component({
  selector: 'app',
  template: `
    Last updated: {{mydate | amLocal | amDateFormat: 'YYYY-MM-DD HH:mm'}}
  `
})

Prints Last updated 2016-01-24 12:34

amLocale pipe

To be used with amDateFormat pipe in order to change locale.

@Component({
  selector: 'app',
  template: `
    Last updated: {{'2016-01-24 14:23:45' | amLocale:'en' | amDateFormat:'MMMM Do YYYY, h:mm:ss a'}}
  `
})

Prints Last updated: January 24th 2016, 2:23:45 pm

Note: The locale might have to be imported (e.g. in the app module).

import 'moment/locale/de';

amFromUnix pipe

@Component({
  selector: 'app',
  template: `
    Last updated: {{ (1456263980 | amFromUnix) | amDateFormat:'hh:mmA'}}
  `
})

Prints Last updated: 01:46PM

amDuration pipe

@Component({
  selector: 'app',
  template: `
    Uptime: {{ 365 | amDuration:'seconds' }}
  `
})

Prints Uptime: 6 minutes

amDifference pipe

@Component({
  selector: 'app',
  template: `
    Expiration: {{nextDay | amDifference: today :'days' : true}} days
  `
})

Prints Expiration: 1 days

amAdd and amSubtract pipes

Use these pipes to perform date arithmetics. See momentjs docs for details.

@Component({
  selector: 'app',
  template: `
    Expiration: {{'2017-03-17T16:55:00.000+01:00' | amAdd: 2 : 'hours' | amDateFormat: 'YYYY-MM-DD HH:mm'}}
  `
})

Prints Expiration: 2017-03-17 18:55

@Component({
  selector: 'app',
  template: `
    Last updated: {{'2017-03-17T16:55:00.000+01:00' | amSubtract: 5 : 'years' | amDateFormat: 'YYYY-MM-DD HH:mm'}}
  `
})

Prints Last updated: 2012-03-17 16:55

amFromUtc pipe

Parses the date as UTC and enables mode for subsequent moment operations (such as displaying the time in UTC). This can be combined with amLocal to display a UTC date in local time.

@Component({
  selector: 'app',
  template: `
    Last updated: {{ '2016-12-31T23:00:00.000-01:00' | amFromUtc | amDateFormat: 'YYYY-MM-DD' }}
  `
})

Prints Last updated: 2017-01-01

It's also possible to specify a different format than the standard ISO8601.

@Component({
  selector: 'app',
  template: `
    Last updated: {{ '31/12/2016 23:00-01:00' | amFromUtc: 'DD/MM/YYYY HH:mmZZ' | amDateFormat: 'YYYY-MM-DD' }}
  `
})

Or even an array of formats:

@Component({
  selector: 'app',
  template: `
    Last updated: {{ '31/12/2016 23:00-01:00' | amFromUtc: formats | amDateFormat: 'YYYY-MM-DD' }}
  `
})
export class App {

  formats: string[] = ['DD/MM/YYYY HH:mm:ss', 'DD/MM/YYYY HH:mmZZ'];

  constructor() { }

}

Both examples above will print Last updated: 2017-01-01

amUtc pipe

Enables UTC mode for subsequent moment operations (such as displaying the time in UTC).

@Component({
  selector: 'app',
  template: `
    Last updated: {{ '2016-12-31T23:00:00.000-01:00' | amUtc | amDateFormat: 'YYYY-MM-DD' }}
  `
})

Prints Last updated: 2017-01-01

amParseZone pipe

Parses a string but keeps the resulting Moment object in a fixed-offset timezone with the provided offset in the string.

@Component({
  selector: 'app',
  template: `
    Last updated: {{ '2016-12-31T23:00:00.000-03:00' | amParseZone | amDateFormat: 'LLLL (Z)' }}
  `
})

Prints Last updated: Saturday, December 31, 2016 11:00 PM (-03:00)

amIsBefore and amIsAfter pipe

Check if a moment is before another moment. Supports limiting granularity to a unit other than milliseconds, pass the units as second parameter

@Component({
  selector: 'app',
  template: `
    Today is before tomorrow: {{ today | amIsBefore:tomorrow:'day' }}
  `
})

Prints Today is before tomorrow: true

@Component({
  selector: 'app',
  template: `
    Tomorrow is after today: {{ tomorrow | amIsAfter:today:'day' }}
  `
})

Prints Tomorrow is after today: true

NgxMomentOptions

An NgxMomentOptions object can be provided to the module using the forRoot convention and will provide options for the pipes to use with the moment instance, these options are detailed in the table below:

prop type description
relativeTimeThresholdOptions Dictionary
key: string
value: number
Provides the relativeTimeThreshold units allowing a pipe to set the moment.relativeTimeThreshold values.

The key is a unit defined as one of ss, s, m, h, d, M.

See Relative Time Thresholds documentation for more details.

Complete Example

import { NgModule, Component } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { MomentModule } from 'ngx-moment';

@Component({
  selector: 'app',
  template: `
    Last updated: <b>{{myDate | amTimeAgo}}</b>, <b>{{myDate | amCalendar}}</b>, <b>{{myDate | amDateFormat:'LL'}}</b>
  `
})
export class AppComponent {
  myDate: Date;

  constructor() {
    this.myDate = new Date();
  }
}

@NgModule({
  imports: [
    BrowserModule,
    MomentModule
  ],
  declarations: [ AppComponent ]
  bootstrap: [ AppComponent ]
})
class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

Demo

See online demo on StackBlitz

changelog

Changelog

6.0.2 - 2021-12-22

  • Fix moment import via jsnext entry point (use default import interop) (see #245)

6.0.1 - 2021-12-20

  • Fix build issue (see #273)_

6.0.0 - 2021-11-30

  • Support for Angular 13 + Ivy

5.0.0 - 2020-07-03

  • fix: support for Angular 10 (see #240)
  • breaking: drop support for Angular < 7

4.0.1 - 2020-06-12

  • republish to npm without ngcc backup files

4.0.0 - 2020-06-12

  • fix: correct input types for pipes 906e40c
  • chore(deps): angular 9, typescript 3.8 9966a9a
  • chore: reformat all code with prettier 10fe5c51
  • docs: remove system.js instructions from README

3.5.0 - 2019-11-08

  • feat: add formatFn argument to amTimeAgo (#213, contributed by chaoyangnz)
  • feat: add format|formats to from-utc pipe and parse-pipe (#215, contributed by gigadie)
  • fix: improve amLocale compability with Angular Ivy (#226, contributed by sobanieca)

3.4.0 - 2019-03-07

  • feat: add amIsBefore, amIsAfter pipes (#208, contributed by StickNitro)
  • Ability to provide options to the MomentModule (#209, contributed by StickNitro)
  • fix: Improved Text Update on locale change when not using MomentInput (#210, contributed by jensweigele)

3.3.0 - 2018-12-09

  • fix: Allow using all supported Moment inputs with TimeAgoPipe (#206, contributed by theodorejb)

3.2.0 - 2018-10-30

  • Angular 7 support (#203)

3.1.0 - 2018-07-01

3.0.1 - 2018-06-07

  • Update installation instruction in readme (see #194)

3.0.0 - 2018-06-02

  • Make moment a peerDependency instead of dependency (see #149 for discussion)

When upgrading to this version, make sure to npm install --save moment.

2.0.0 - 2018-04-25

2.0.0-beta.0 and 2.0.0-rc.0 are aliases for this version.

1.9.0 - 2018-05-03

  • Fix: update momentjs version (#190)
  • Fix: amTimeAgo pipe updates it's output when locale changes (#188, contributed by lukasz-kusnierz)

1.8.0 - 2018-01-23

  • Fix: Remove node_modules from compiled version (#187, contributed by wachri)

1.7.1 - 2017-12-22

  • Fix bug where FromUtcPipe was being imported instead of exported (#180, contributed by fshin123)
  • test: fix amLocal tests to pass regardless of timezone

1.7.0 - 2017-08-19

1.6.0 - 2017-07-18

1.5.0 - 2017-07-14

  • Add amLocale pipe (#155, contributed by FallenRiteMonk)
  • Migrate testing framework to jest

1.4.0 - 2017-06-18

  • Add amParse pipe to enable parsing of custom-formatted date string (#148, contributed by vin-car)

1.3.3 - 2017-03-18

  • Fix: amCalendar causes protractor to timeout on waiting async Angular (#135, contributed by romanovma)

1.3.2 - 2017-03-17

  • Fix: Add missing amAdd and amSubtract pipes to the NgModule (#134, contributed by datencia)

1.3.1 - 2017-03-16

  • Add missing amAdd and amSubtract pipes (fixes #130)

1.3.0 - 2017-03-10

  • Enable Angular 4 as peer dependency

1.2.0 - 2017-02-09

1.1.0 - 2017-01-09

Happy new year!

  • Add referenceTime and format args to amCalendar (#64, contributed by irsick)
  • Add amAdd and amSubtract pipes (#113, contributed by dustin486)
  • Fix: Do not import whole Rx.js library (#117, contributed by FabienDehopre)

1.0.0 - 2016-12-01

Promoted 1.0.0-rc.1 to final release

1.0.0-rc.1 - 2016-11-11

*** Breaking change: Requires moment 2.16.0 or newer

  • Fix “Expression has changed after it was checked” (#111, contributed by nithril)
  • Fix "Module 'moment' has no exported member 'UnitOfTime'" (#112)

1.0.0-beta.6 - 2016-10-24

*** Breaking change: typescript sources are no longer published in the npm package

  • Inline sources in the source map file, should fix #96.
  • Handle undefined dates in amDateFormat pipe (#105, contributed by amcdnl)

1.0.0-beta.5 - 2016-10-13

*** Breaking change: source files renamed, which could affect your imports:

import { TimeAgoPipe } from 'angular-moment/TimeAgoPipe';

now becomes:

import { TimeAgoPipe } from 'angular-moment/time-ago.pipe';

All changes:

  • Rename source files to follow Angular 2 Style Guide conventions
  • Require moment >= 2.13.0, and remove @types/moment from our dependencies (as it is already included in moment)

1.0.0-beta.4 - 2016-10-06

  • Add support for server side pre-rendering (#89, contributed by https://github.com/jmezach)
  • Fix a bug caused TimeAgo and Calendar pipes not to update automatically (#94)
  • Add @types/moment to package dependencies (see #91)

1.0.0-beta.3 - 2016-10-04

  • Fix exports for Rollup / Ionic 2 users (#86, contributed by TheMadBug)
  • Protractor fix: run long standing timeouts outside of angular zones (#74, contributed by tiagoroldao)

1.0.0-beta.2 - 2016-10-01

  • Switch to Typescript 2.0
  • Angular 2 AoT (Ahead of Time) template compilation support (#68)
  • Removed impure flags from pure Pipes: amDateFormat and amDifference (#75, contributed by tiagoroldao)

1.0.0-beta.1 - 2016-08-16

  • Support angular-2.0.0-rc.5 NgModules, see README for details.

0.8.2 - 2016-08-01

  • Add amDifference pipe (#54, contributed by josx)

0.8.1 - 2016-07-03

  • Add omitSuffix parameter to amTimeAgo pipe (#47, contributed by bzums)

0.8.0 - 2016-05-22

  • Publish typescript sources under src folder, should fix Ionic 2 issues such as #28 and #33.

0.7.0 - 2016-05-03

  • Align with the angular 2.0.0-rc.0 and the new angular packaging system

0.6.0 - 2016-04-28

  • Align with angular 2.0.0-beta.16 (#32, contributed by fknop)

0.5.0 - 2016-04-08

  • Move angular2 from npm dependencies to peerDependencies (see #24)
  • Add amDuration pipe (#29, contributed by xenolinguist)

0.4.3 - 2016-03-06

  • include amFromUnix pipe in the package's exports
  • publish our typings.json to npm

0.4.2 - 2016-02-24

0.4.1 - 2016-02-21

  • Don't run typings install on postinstall (fixes #13)

0.4.0 - 2016-02-16

  • Switch from tsd to typings, stop publishing the moment.js typings to npm.
  • Additional unit-tests

Note: You may need to manually install moment.js typings, by running typings install --save moment in your project directory.

0.3.0 - 2016-01-27

  • add amDateFormat pipe (#9, contributed by andreialecu)
  • refactor: remove the supports() from all the pipes (it is no longer used as of angular2-beta)

0.2.1 - 2016-01-16

  • bugfix: wrong method name for cleanup, caused resource leak (#8, contributed by andreialecu)

0.2.0 - 2016-01-12

0.1.1 - 2015-12-18

  • Fix 'Cannot use in app due to triple-slash references' typescript error (#2)

0.1.0 - 2015-12-15

  • Align with angular 2.0.0-beta.0

0.0.5 - 2015-11-12

  • Align with angular-2.0.0-alpha.46

0.0.4 - 2015-10-25

  • Add ES5 transpiled version and typescript definitions (.d.ts) file to the published npm package

0.0.3 - 2015-10-22

  • Align with angular-2.0.0-alpha.44

0.0.2 - 2015-09-18

  • Align with angular-2.0.0-alpha.37

0.0.1 - 2015-08-25

  • Initial release