To use the pipes, import DateFnsModule into your Angular component or module. You can then use various dfns* pipes in your templates to format, parse, or manipulate dates.
Example usage in a standalone component:
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { es } from 'date-fns/locale';
import { DateFnsModule } from 'ngx-date-fns';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, DateFnsModule],
template: `
<p>{{ dateOne | dfnsFormat: 'MM/dd/yyyy' }}</p>
<p>{{ [dateOne, dateTwo] | dfnsMin | dfnsFormat: 'EEE LLLL d yyyy' }}</p>
<p>
{{ dateThree | dfnsFormatDistanceToNow: options }} - (Explicit 'es' locale)
</p>
"
})
export class App {
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);
}
}
bootstrapApplication(App);