Reputation:
I have a service that obtains certain data from a BehaviorSubject.
this.myData.album$.value.album_date;
When console logging the returned date from firestore with:
console.log(this.myData.album$.value.album_date);
The console reads:
Timestamp {seconds: 1572263054, nanoseconds: 63000000}
What I need to do is convert this value inside one of my functions and manipulate it into any format, such as "Monday, 28th October 2019".
Unfortunately given my requirement i cannot use the toDate() |
inside the HTML and this needs to be achieved within my function.
Any assistance here would be greatly appreciated.
Note: I have moment
available to use within my Typescript file.
I am using Angular 8
and Firestore
Upvotes: 0
Views: 949
Reputation: 1177
A little bit late to the party, but I've encountered the same issue.
Here is my solution, although not pretty because of the use of any
, but it worked for me:
(myAlbumDate as any).toDate()
Now TypeScript doesn't complain anymore and the production building passes without errors.
Update
Today I found myself in the same scenario and because the any
type scratches my brain I tried to find a more elegant solution and I found it on another StackOverflow question and I applied it to my use case.
The solution is to extend the Date
interface like so:
declare global {
interface Date {
toDate: () => Date;
}
}
Now I have IntelliSense and no more any
or errors in my Angular template.
Upvotes: 2
Reputation: 6271
https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#to-date
You need to use Firebase to get the date within your function, and then pass the date to your html:
console.log(this.myData.album$.value.album_date.toDate());
I think may work
Upvotes: 0