Created
July 20, 2022 18:39
-
-
Save riapacheco/8bb2d8cd447111f056ae8c6c210436d3 to your computer and use it in GitHub Desktop.
Firestore Date Pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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