0
votes

I have this field in firebase "cloud firestore" firebase date field

and in cloud functions I want to extract (get only the year from this date, the year alone and month alone)

  const year = BookedSessionChanges.bookedAt.toDate();
    console.log("yearr ", year);
    const month = BookedSessionChanges.bookedAt.toDate();
    console.log("month ", month);

here's the code in cloud function, where i used toDate() to convert the date shown in above image to look like 2020-10-06T08:38:14.066Z time beside it.

So how can I get only the year and then the month from this date? Ps. I tried getYear() and toDate("Y") ... but none of these works.

Any help? Thanks

1

1 Answers

0
votes

toDate() method converts Firestore Timestamp object to a Javascript Date object. After conversion you can use getYear() method to extract the year.

const year = BookedSessionChanges.bookedAt.toDate().getYear();