0

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

Que
  • 957
  • 2
  • 14
  • 35

2 Answers2

2

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.

ionut-t
  • 1,121
  • 1
  • 7
  • 14
0

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

K20GH
  • 6,032
  • 20
  • 78
  • 118
  • 1
    An update - This works in development environment. However the 'toDate()' is underlined in red by typescript. The error being `Property 'toDate' does not exist on type 'Date'.ts(2339)`. Building in production fails unfortunately. – Que Oct 28 '19 at 12:22