• Stars
    star
    1,174
  • Rank 38,477 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

moment.js pipes for Angular

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

More Repositories

1

angular-moment

Moment.JS directives for Angular.JS (timeago and more)
JavaScript
2,609
star
2

angular-spinner

Angular directive to show an animated spinner (using spin.js)
TypeScript
697
star
3

firebase-server

Firebase Realtime Database Server Implementation
TypeScript
674
star
4

muse-js

Muse 2016 EEG Headset JavaScript Library (using Web Bluetooth)
TypeScript
267
star
5

angular-load

Dynamically load scripts and css stylesheets in your Angular.JS app
JavaScript
242
star
6

angular-iot

Internet of Things directives for Angular 2 (Led, Button, etc.)
TypeScript
95
star
7

gulp-add-src

Add more 'src' files at any point in the pipeline (gulp plugin)
JavaScript
86
star
8

web-bluetooth-polyfill

Windows 10 Web Bluetooth Polyfill
JavaScript
76
star
9

web-ar-experiment

Experimenting with WebAR
HTML
68
star
10

arduino-slack-bot

A Real Time Slack Bot for Arduino (ESP8266)
Arduino
64
star
11

win-ble-cpp

Proof of Concept - BLE on Windows (for Web Bluetooth)
C++
63
star
12

pdfium-wasm

Building PDFium for Web Assembly
Dockerfile
61
star
13

ng2-simon

Simon Game for angular2-iot
Eagle
58
star
14

bigtsquery

Search Engine for TypeScript Code using AST Queries
TypeScript
50
star
15

web-lightbulb

Smart Light Bulb Web App (using Web Bluetooth)
JavaScript
49
star
16

muse-blink

Blink detection with Muse EEG Headset + Angular demo
TypeScript
43
star
17

angular-placeholder-shim

Angular directive to make the input/textarea placeholder attribute work on all browsers.
JavaScript
41
star
18

tsquery-playground

Playground for TSQuery
TypeScript
37
star
19

muse-lsl

Muse 2016 EEG Headset LSL (NodeJS)
JavaScript
32
star
20

purple-eye

A web-bluetooth controlled one-eyed robot
C++
26
star
21

ng-beacon

ng-beacon - Smart JavaScript Beacon shaped after the Angular logo
Eagle
24
star
22

angular2-material-build

Material Design for Angular2
JavaScript
22
star
23

angular-static-site

Static Website with Angular - Build time rendering
TypeScript
20
star
24

beat-machine

Salsa Beat Machine 5 (Next.js + Web Audio)
TypeScript
20
star
25

node-lsl

LibLSL bindings for Node.js
JavaScript
20
star
26

cinto

Python
20
star
27

awesome-web-bluetooth

Awesome list of Web Bluetooth Libraries, Demos and Resources
17
star
28

ng-lift

Automated tooling for upgrading Angular.js app to Angular
TypeScript
17
star
29

vscode-tsquery

TSQuery extension for Visual Studio Code
TypeScript
17
star
30

AndroidThingsCounter

Android Things app that uses a 7-Segment to display counter
Java
16
star
31

web-bluetooth-mock

Mock Implementation of Web Bluetooth (for tests)
TypeScript
15
star
32

aramcon-badge

Just a badge
Python
15
star
33

pdfium-wasm-example

Example of using PDFium Web Assembly build in Node.js
JavaScript
15
star
34

real-trex-runner

IRL version of Chrome Offline T-Rex game
JavaScript
13
star
35

heart-ariella

Heart-shaped flashlight PCB for Ariella
Eagle
12
star
36

java-openal

OpenAL JNA Wrappers for Java
Java
12
star
37

vr-hero-aframe

VR Hero: Invaders game with Angular + A-Frame
TypeScript
11
star
38

d3-angularjs-demo

Demo code for my talk: "Visualizing Data streams using Angular & D3.js"
JavaScript
11
star
39

TiMedia

AVFoundation wrappers for Titanium
Objective-C
11
star
40

angular-aframe-pipe

A-Frame pipe for interop with Angular
TypeScript
11
star
41

solidity-contracts

A collection of solidity contracts found on Github
JavaScript
11
star
42

webble-mip

MiP Robot Web Bluetooth App
HTML
10
star
43

circuitpython-mp3-ble

Streaming MP3 over BLE using CircuitPython and VS1053
Python
10
star
44

panda

Panda Logo PCB
Batchfile
9
star
45

angular-promises-toolkit

Extends the Angular.JS promise API with useful methods
JavaScript
9
star
46

aframe-fireball-component

Fireball component for A-Frame
JavaScript
8
star
47

love

Because branches also need some love!
8
star
48

gwt-titanium

A GWT module for Appcelerator's Titanium Platform
Java
8
star
49

t-rex-vr

VR Experiment T-Rex Game
JavaScript
8
star
50

ZlSound

Zero Latency Sound module for Titanium
Objective-C
8
star
51

nn-function-generator

Experimenting with automatic generation of TS function bodies using ANN models
TypeScript
8
star
52

ml-comments-gen

Generating source code comments with Machine Learning
TypeScript
8
star
53

tiny-simon

Simon Says game for ATTiny85
C++
8
star
54

aframe-camera-events

A-Frame helper to emit events when camera position / orientation changes
JavaScript
7
star
55

ng-beacon-app

Angular app to control ng-beacons
TypeScript
7
star
56

noble-winrt

WinRT bindings for Noble (using BLEServer.exe)
JavaScript
7
star
57

ctf-shittyaddon

Capture The Flag Shitty Addon
C++
7
star
58

typewiz-webpack-demo

Demo of using TypeWiz with WebPack
JavaScript
6
star
59

m3d-tools

Command line tools for interacting with the M3D Printer
C#
6
star
60

battery-bulb

Battery Powered Magic Blue bulb
5
star
61

angular-es6-promises

ECMAScript 6 promises for Angular.JS
JavaScript
5
star
62

trumpet-nuggets

Web MIDI Snippets created by attendees during Chrome Dev Summit 2018
TypeScript
5
star
63

web-bluetooth-mamush

Live coding Web Bluetooth, a smart light bulb, Muse EEG headset and Chrome T-Rex game with React
JavaScript
5
star
64

spinduino-js

POV Fidget spinner running JavaScript
JavaScript
5
star
65

web-bluetooth-secure-dfu

nRF5x Secure DFU over Web Bluetooth PoC
JavaScript
5
star
66

magicblue

Control Magic Blue Bulbs using Web Bluetooth
TypeScript
4
star
67

android-hello-arm

Hello world in Android ARM assembly
Batchfile
4
star
68

m3d-pro-gcodes

GCodes and Troubleshooting for the M3D Pro
4
star
69

hot-or-not

Hot or Not game for ng-beacons
TypeScript
4
star
70

elm-smart-bulb

Initial PoC of controlling a Smart Bluetooth Bulb from Elm code using Web Bluetooth
Elm
4
star
71

valentines-vr

Small VR heart-collecting game for Valentines, live-coding during a IoT Makers meetup using A-Frame
HTML
4
star
72

vr-audio-visualizer

Audio Visualization Experiment with Angular + A-Frame
TypeScript
4
star
73

bluetooth-buzzer-button

Bluetooth Smart Buzzer Button, programmable with JavaScript
JavaScript
4
star
74

ng-beacon-firmware

ng-beacon firmware builds
4
star
75

particle-mesh-broadcast

Sample code for broadcasting a message over the local mesh network using UDP
C++
3
star
76

web-midi-playground

Web MIDI Playground for Chrome Dev Summit demo
TypeScript
3
star
77

bitcoin-action-express

"Bitcoin Info" Assistant Action with TypeScript + Express
TypeScript
3
star
78

web-bluetooth-typings

Typings for Web Ble
TypeScript
3
star
79

ng-beacon-firmware-old

JavaScript Firmware for ng-beacon (based on Espruino)
Shell
3
star
80

espruino-mirror

Cast Espruino Pixl.js Display over BLE connection
TypeScript
3
star
81

doggie

A simple android watchdog application
Java
3
star
82

ng2-simon-highscore

App to display ng2-simon highscore on a TV
TypeScript
3
star
83

ac-generators

AngularConnect Generators + Protractor demo (TopCat)
JavaScript
3
star
84

t-rex-blinks

Chrome T-Rex Game controlled by eye blinks
JavaScript
3
star
85

firebase-localstorage-fix

Firebase 3.1.0 with support for disabled LocalStorage (e.g. private browsing in Safari)
JavaScript
3
star
86

iphonejava-hello-world

Hello world iPhone application written in Java!
Java
3
star
87

protractor-firebase

Utilities for interacting with Firebase within protractor tests
JavaScript
3
star
88

pi-thingy52-weather

Weather Station using Raspberry Pi, Thingy52 and HSDPA dongle
JavaScript
3
star
89

extract-java-classes

Extract all Java class definitions from the Github dataset
JavaScript
2
star
90

coronavirus-3d

Corona Virus 3D
HTML
2
star
91

ng-pitfalls-demos

Code samples from my presenetation "Avoiding Common Pitfalls in AngularJS"
JavaScript
2
star
92

ng2-topcat

Angular 2 + Firebase Cat Rating System
TypeScript
2
star
93

muse-systemjs

Muse EEG Headset + SystemJS Minimal Setup
HTML
2
star
94

vr-talk-assets

Assets for WebVR talk
TypeScript
2
star
95

solidity-downloader

Downloads a bunch of Solidity Contracts from GitHub Repos
JavaScript
2
star
96

serial-7segment-20mm

PCB + Firmware for 20mm Serial 7-Segment Display
Eagle
2
star
97

lightshow-poc

JavaScript
2
star
98

aramcon-broadcast

Broadcast MP3 Data over ESB (Enhanced ShockBurst) using nRF52840 + USB CDC
C
2
star
99

ble-pressure-sensor

Bluetooth Low Energy Pressure Sensor with Espruino
TypeScript
2
star
100

sa2015-topcat

Topcat
JavaScript
2
star