项目作者: Aaron-Sterling

项目描述 :
Angular pipes based on date-fns. Date pipe, time-ago pipe, minDate pipe, maxDate pipe, distanceBetweenDates pipe.
高级语言: TypeScript
项目地址: git://github.com/Aaron-Sterling/ng-datefns-pipes.git
创建时间: 2018-01-22T01:14:17Z
项目社区:https://github.com/Aaron-Sterling/ng-datefns-pipes

开源协议:

下载


ng-datefns-pipes

Angular pipes based on date-fns. Date pipe, time-ago pipe, minDate pipe, maxDate pipe, distanceBetweenDates pipe. I initially built these for Ionic projects, because the Angular date pipe was not performing well on all mobile devices, and the date-fns functions were not exhibiting problems. However, no use of Ionic is required. This npm library should work with any Angular 5 project, and any Ionic 3 project.

  1. Installation
  2. Sample usage
  3. Basic setup
  4. Advanced setup
  5. API

Next page: Non-English locales

Installation

English locale:

npm install ng-datefns-pipes --save

Support for all date-fns locales:

npm install ng-datefns-pipes-all-locales --save

Sample usage

Date pipe:

  1. napoleon = new Date(1804, 11, 2); // in ts file
  2. Napoleon crowned himself emperor on {{ napoleon | date }}. // in template
  3. Napoleon crowned himself emperor on December 2nd, 1804. // output

Time ago pipe:

  1. aquinas = new Date(1274, 2, 7); // in ts file
  2. Aquinas died {{ aquinas | ago }}.<br> // in template
  3. Aquinas died almost 744 years ago. // output

Time ago pipe second example:

  1. now = new Date(); // in ts file
  2. You entered this page {{ now | ago }}. // in template
  3. You entered this page less than 5 seconds ago. // output (updates every 15 seconds)

Distance between dates:

  1. datePair = [ this.aquinas, this.napoleon ]; // in ts file
  2. Aquinas's and Napoleon's actions were separated by {{ datePair | distanceBetweenDates }}. // in template
  3. Aquinas's and Napoleon's actions were separated by almost 531 years. // output

Min date:

  1. dateArray = [ this.aquinas, this.napoleon, this.now ]; // in ts file
  2. The earliest of those dates is {{ dateArray | minDate }}. // in template
  3. The earliest of those dates is March 7th, 1274. // output

Max date:

  1. The most recent of those dates is {{ dateArray | maxDate }}. // in template (ts file same as before)
  2. The most recent of those dates is ___. // <-- output, blank contains today's date

Basic setup

In app.module.ts:

  1. import { DatePipesModule } from 'ng-datefns-pipes';
  2. @NgModule({
  3. declarations: [],
  4. imports: [
  5. DatePipesModule.forRoot() // < -- add this line
  6. ],
  7. bootstrap: [],
  8. entryComponents: [],
  9. providers: []
  10. })
  11. export class AppModule {}

This gives you access to all the (English language) date pipes, with the default configuration. For details, see the API below.

Advanced Setup

You can specify a default date format; whether date distance functions include seconds; and the prefix, suffix, refresh rate and use of seconds of the timeAgo pipe. (For more detail about these settings, see the API below. You can also set and reset each setting during the program by injecting the DatePipeManager.)

In app.module.ts:

  1. import { DatePipesModule, DatePipeConfiguration } from 'ng-datefns-pipes';
  2. const DATE_PIPE_CONFIG: DatePipeConfiguration = {
  3. defaultDateFormat: 'your date format string', // default: 'MMMM Do, YYYY'
  4. dateDistanceIncludesSeconds: your boolean here, // default: false
  5. agoPipeOptions: {
  6. refreshRate: your milliseconds here, // default: 15000
  7. prefix: 'your prefix string', // default: ''
  8. suffix: 'your suffix string', // default: ' ago' <-- note the space
  9. includeSeconds: your boolean here // default: false
  10. }
  11. }
  12. @NgModule({
  13. declarations: [],
  14. imports: [
  15. DatePipesModule.forRoot(DATE_PIPE_CONFIG) // < -- add this line
  16. ],
  17. bootstrap: [],
  18. entryComponents: [],
  19. providers: []
  20. })
  21. export class AppModule {}

API

  1. Date pipe
  2. Time ago pipe
  3. Distance between dates pipe
  4. MaxDate pipe
  5. MinDate pipe

Next page: Non-English locales