All Projects → joanllenas → Ngx Date Fns

joanllenas / Ngx Date Fns

⏳ date-fns pipes for Angular 2.0 and above ⏳

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Date Fns

awesome-angular
💖 A list of awesome Angular (2️⃣➕) resources
Stars: ✭ 61 (-54.81%)
Mutual labels:  angular2, ng2, ngx
Ngx Echarts
An angular (ver >= 2.x) directive for ECharts (ver >= 3.x)
Stars: ✭ 820 (+507.41%)
Mutual labels:  ng2, angular2, ngx
ng-elm
Write Angular components in Elm.
Stars: ✭ 41 (-69.63%)
Mutual labels:  angular2, ng2, ngx
Angular2 Carousel
An lightweight , touchable and responsive library to create a carousel for angular 2 / 4 / 5
Stars: ✭ 26 (-80.74%)
Mutual labels:  ng2, angular2, ngx
Ngx Cookie Service
Angular (4.2+ ...11) service for cookies. Originally based on the `ng2-cookies` library.
Stars: ✭ 363 (+168.89%)
Mutual labels:  angular2, ngx
Angular
UI-Router for Angular: State-based routing for Angular (v2+)
Stars: ✭ 287 (+112.59%)
Mutual labels:  ng2, angular2
Cc
一个基于angular5.0.0+ng-bootstrap1.0.0-beta.8+bootstrap4.0.0-beta.2+scss的后台管理系统界面(没基础的同学请先自学基础,谢谢!)
Stars: ✭ 416 (+208.15%)
Mutual labels:  ng2, angular2
Ngx Dnd
🕶 Drag, Drop and Sorting Library for Angular2 and beyond!
Stars: ✭ 511 (+278.52%)
Mutual labels:  angular2, ngx
Ngx Admin
Customizable admin dashboard template based on Angular 10+
Stars: ✭ 23,286 (+17148.89%)
Mutual labels:  ng2, angular2
Ng2 Smart Table
Angular Smart Data Table component
Stars: ✭ 1,590 (+1077.78%)
Mutual labels:  ng2, angular2
Ng2 Ace
A basic ace editor directive for angular 2.
Stars: ✭ 33 (-75.56%)
Mutual labels:  ng2, angular2
Ngx Scroll To
Scroll to any element to enhance scroll-based features in you app. Works for Angular 4+, both AoT and SSR. No dependencies.
Stars: ✭ 269 (+99.26%)
Mutual labels:  angular2, ngx
Ngx Drag To Select
A lightweight, fast, configurable and reactive drag-to-select component for Angular 6 and beyond
Stars: ✭ 262 (+94.07%)
Mutual labels:  angular2, ngx
Core
The internationalization (i18n) library for Angular
Stars: ✭ 4,027 (+2882.96%)
Mutual labels:  angular2, ngx
ngx-magicsearch
Magic Search/Faceted Search Library for Angular 2
Stars: ✭ 19 (-85.93%)
Mutual labels:  angular2, ng2
angular2-cookie-law
Angular2+ component that provides a banner to inform users about cookie law
Stars: ✭ 38 (-71.85%)
Mutual labels:  angular2, ng2
Angulartics2
Vendor-agnostic analytics for Angular2 applications.
Stars: ✭ 963 (+613.33%)
Mutual labels:  angular2, ngx
Ng2 Sweetalert2
A sweetalert2 service for angular2.
Stars: ✭ 49 (-63.7%)
Mutual labels:  ng2, angular2
ng2-gravatar
Angular2 gravatar directive
Stars: ✭ 21 (-84.44%)
Mutual labels:  angular2, ng2
ng2-fused
FuseBox plugins and utilities for building Angular2 applications.
Stars: ✭ 13 (-90.37%)
Mutual labels:  angular2, ng2

+ = ngx-date-fns

Build Status codecov npm version Known Vulnerabilities Conventional Commits

date-fns pipes for Angular 2.0 and above.

Table Of Contents

Installation

This library requires date-fns >= v2.16.0.

⚠️ We recommend pinning your date-fns version to 2.16.1 until this issue, which prevents tree shaking from working, is fixed.

  • npm install --save date-fns
  • npm install --save ngx-date-fns

date-fns v1 support

Basic Usage

Import DateFnsModule into your app's module:

import { DateFnsModule } from 'ngx-date-fns';

@NgModule({
  imports: [
    // (...)
    DateFnsModule.forRoot()
  ]
})

In lazy loaded module declarations use imports: [DateFnsModule].

import { Component } from '@angular/core';
import { es } from 'date-fns/locale';

@Component({
  selector: 'my-component',
  template: `
    <p>{{ dateOne | dfnsFormat: 'yyyy/MM/dd' }}</p>
    <p>{{ [dateOne, dateTwo] | dfnsMin }}</p>
    <p>{{ [dateOne, dateTwo] | dfnsMax | dfnsFormat: 'EEE LLL d yyyy' }}</p>
    <p>{{ dateThree | dfnsFormatDistanceToNow: options }}</p>
  `
})
export class AppComponent {
  dateOne = new Date(2016, 0, 1);
  dateTwo = new Date(2017, 0, 1);
  dateThree: Date;
  options = {
    locale: es,
    addSuffix: true
  };
  constructor() {
    this.dateThree = new Date();
    this.dateThree.setDate(this.dateThree.getDate() - 6);
  }
}

The output:

2016/01/01

Fri Jan 01 2016 00:00:00 GMT+0100 (Central European Standard Time)

Sun January 1 2017

hace 6 días

Working with locales

All locale-aware pipes use English by default.

Changing locale globally

Instead of passing the locale to each pipe via options you can set it globally in one single step by overriding the default DateFnsConfiguration implementation:

import { DateFnsModule } from 'ngx-date-fns';
import { fr } from "date-fns/locale";

const frenchConfig = new DateFnsConfigurationService();
frenchConfig.setLocale(fr);

@NgModule({
  imports: [
    // (...)
    DateFnsModule.forRoot()
  ],
  providers: [
    // (...)
    { provide: DateFnsConfigurationService, useValue: frenchConfig } // <-- All pipes in French by default
  ]
})

Changing locale at runtime

It is also possible to change the default locale at runtime:

import { Component } from '@angular/core';
import { DateFnsConfigurationService } from '../lib/src/date-fns-configuration.service';
import { es, de } from 'date-fns/locale';

@Component({
  selector: 'app-root',
  template: `
    <p>{{ dateOne | dfnsFormat: 'MM/dd/yyyy' }}</p>
    <hr />
    Set default locale to: <a  (click)="changeToGerman()">German</a>,
    <a  (click)="changeToSpanish()">Spanish</a>.
  `
})
export class AppComponent {
  dateOne = new Date(2016, 0, 1);
  constructor(public config: DateFnsConfigurationService) {}
  changeToGerman() {
    this.config.setLocale(de);
  }
  changeToSpanish() {
    this.config.setLocale(es);
  }
}

Pure or impure?

Should I use the pure or impure version of the locale-aware pipes?

The answer is quite simple:

  • Do you set the locale only once when the application starts?
    • Use only pure pipes.
  • Do you need to change the locale at runtime?
    • Use impure pipes.

The main difference is that pure pipes do not get notified when the locale is changed via DateFnsConfiguration.setLocale(locale: Locale), because the instance is not kept in memory. Impure pipes, on the other hand, are kept in memory and listen for Locale change notifications, which adds some memory and performance overhead.

Tree shaking date-fns

⚠️ We recommend pinning your date-fns version to 2.16.1 until this issue, which prevents tree shaking from working, is fixed.

The library itself is optimized to be tree-shakable by just importing DateFnsModule.forRoot() or selectively import pipes by calling them from ngx-date-fns package itself, as those were exported following the SCAM structure, for example:

// app.module.ts
import { fr } from 'date-fns/locale';
import { DateFnsConfigurationService } from 'ngx-date-fns';
import {
  FormatPipeModule,
  MinPipeModule,
  MaxPipeModule,
  FormatDistanceToNowPipeModule,
  WeekdayNamePipeModule,
  ParsePipeModule
} from 'ngx-date-fns';

const frenchConfig = new DateFnsConfigurationService();
frenchConfig.setLocale(fr);

@NgModule({
  // ... other module stuff
  imports: [
    // Selectively import
    FormatPipeModule,
    MinPipeModule,
    MaxPipeModule,
    FormatDistanceToNowPipeModule,
    WeekdayNamePipeModule,
    ParsePipeModule
  ],
  providers: [{ provide: DateFnsConfigurationService, useValue: frenchConfig }]
})
export class AppModule {}

You can test this by downloading this repo and running:

npm run analyze:app

This command will load a file in your browser where you will see that date-fns takes 63Kb, which is significantly less than the 286Kb of the whole library without tree shaking applied. (this, of course, will be much less after gzipping).

Also take into account that locale files tend to increase the final bundle size of date-fns as well.

Available pipes

All pipes are pure unless stated otherwise.

Misc

Format

Since v7.0.0 invalid Dates will return an empty string instead of throwing an exception.

Get

Difference

Add

Subtract

End

Start

Last Day Of

Is...?

Utils

A collection of utilities built around date-fns functions.

dfnsWeekdayName

This pipe is (impure)

Given a weekday number, returns its name.

@param WeekdayNameFormat

Optional weekday format. Allowed values are:

  • x1: One char. 'M' for Monday.
  • x2: Two chars. 'Mo' for Monday.
  • x3: Three chars. 'Mon' for Monday.
  • full: Full weekday name. 'Monday' for Monday.

Defaults to full.

@param Locale

Optional date-fns Locale object.

Optional locale object.

Basic example

<div>
  <!-- Prints Monday -->
  {{ 0 | dfnsWeekdayName }}
</div>
<div>
  <!-- Prints Mon -->
  {{ 0 | dfnsWeekdayName : 'x3' }}
</div>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].