Skip to content

Instantly share code, notes, and snippets.

@riapacheco
Created July 20, 2022 18:39
Show Gist options
  • Save riapacheco/8bb2d8cd447111f056ae8c6c210436d3 to your computer and use it in GitHub Desktop.
Save riapacheco/8bb2d8cd447111f056ae8c6c210436d3 to your computer and use it in GitHub Desktop.
Firestore Date Pipe
import {formatDate} from '@angular/common';
import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core';
import firebase from 'firebase/app';
import Timestamp = firebase.firestore.Timestamp;
/**
* Can change style of date rendered to 'medium', 'shortDate', etc.
* Change on line 26
*
* Example
* {{ post?.date | firestoreDate }}
*/
@Pipe({
name: 'firestoreDate'
})
export class FirestoreDatePipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private locale: string) {
}
transform(timestamp: Timestamp, format?: string): string {
if (!timestamp?.toDate) {
return;
}
return formatDate(timestamp.toDate(), format || 'medium', this.locale);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment