项目作者: ljmerza

项目描述 :
dayjs pipes for Angular
高级语言: TypeScript
项目地址: git://github.com/ljmerza/ngx-dayjs.git
创建时间: 2018-04-27T17:02:55Z
项目社区:https://github.com/ljmerza/ngx-dayjs

开源协议:MIT License

下载


ngx-dayjs

dayjs pipes for Angular

Build Status

Installation

npm install --save ngx-dayjs

For System.js users:

First you need to install dayjs:

npm install dayjs --save

Don’t forget to update your systemjs.config.js:

  1. packages: {
  2. app: {
  3. main: './main.js',
  4. defaultExtension: 'js'
  5. },
  6. 'dayjs': {
  7. main: './dayjs.js',
  8. defaultExtension: 'js'
  9. },
  10. 'ngx-dayjs': {
  11. main: './index.js',
  12. defaultExtension: 'js'
  13. }
  14. }

Usage

Import dayjsModule into your app’s modules:

  1. import { dayjsModule } from 'ngx-dayjs';
  2. @NgModule({
  3. imports: [
  4. dayjsModule
  5. ]
  6. })

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

Available pipes

amFromUnix pipe

  1. @Component({
  2. selector: 'app',
  3. template: `
  4. Last updated: {{ (1456263980 | amFromUnix) | amDateFormat:'hh:mmA'}}
  5. `
  6. })

Prints Last updated: 01:46PM

amParse pipe

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

  1. @Component({
  2. selector: 'app',
  3. template: `
  4. Last updated: {{'24/01/2014' | amParse:'DD/MM/YYYY' | amDateFormat:'LL'}}
  5. `
  6. })

Prints Last updated: January 24, 2016

amDifference pipe

  1. @Component({
  2. selector: 'app',
  3. template: `
  4. Expiration: {{nextDay | amDifference: today :'days' : true}} days
  5. `
  6. })

Prints Expiration: 1 day

amAdd and amSubtract pipes

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

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

Prints Expiration: 2017-03-17 18:55

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

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

Complete Example

  1. import { NgModule, Component } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
  4. import { dayjsModule } from 'ngx-dayjs';
  5. @Component({
  6. selector: 'app',
  7. template: `
  8. Last updated: <b>{{myDate | amSubtract:timeLeft}}</b>
  9. `
  10. })
  11. export class AppComponent {
  12. myDate: Date;
  13. constructor() {
  14. this.myDate = new Date();
  15. }
  16. }
  17. @NgModule({
  18. imports: [
  19. BrowserModule,
  20. dayjsModule
  21. ],
  22. declarations: [ AppComponent ]
  23. bootstrap: [ AppComponent ]
  24. })
  25. class AppModule {}
  26. platformBrowserDynamic().bootstrapModule(AppModule);