2
  const docRef = await addDoc(collection(db, "question"), {
    title: writingTitle,
    createdAt: serverTimestamp(),
  });

If you write servertimestamp() when you write, the console window says {seconds=123123123, nanosecond=!@!@}} like this. How can I change this to Date on the web?

clauziere
  • 1,323
  • 12
  • 20
love
  • 21
  • 3
  • refer [here](https://stackoverflow.com/questions/34718668/firebase-timestamp-to-date-and-time) – gretal Dec 22 '21 at 06:34

2 Answers2

1

To convert a Firestore timestamp to a date, call toDate() on it after reading the value from the database. For more on this, see the reference docs.

There is no way to get the value before that/without reading it, as serverTimestamp() only generates a token value/sentinel that the database server recognizes as a signal to write the date/time.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

Here's how I do it (with Typescript / React Native):

  const docRef = await addDoc(collection(db, "question"), {
    title: writingTitle,
    createdAt: serverTimestamp(),
  });
  const docSnapshot = await docRef.get();
  //Then get any data from the added object
  const timestamp = docSnapshot.data().createdAt
clauziere
  • 1,323
  • 12
  • 20