user8836786
user8836786

Reputation:

Angular / Firestore - Adding a formatted timestamp to a string field

I'm writing a new document to Firestore. One of the fields must have some text, followed by the date, formatted like so 10/11/2020, 13:07. What would be the simplest way to achieve this? I have an exceptionally convoluted way of achieving this with Moment.js in an old project. But I would like to see if there's a more elegant approach.

this.doc.set({
  title: "A New Title " + ,
  date: firebase.firestore.FieldValue.serverTimestamp(),
})

In the above snippet, the title would need to be something like title: "My New Title " + date" so that the final result is something like: My New Title - 10/11/2020, 13:07

Upvotes: 0

Views: 90

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600090

There is no way to merge a server-side timestamp with another value in a single write.

You will either have to:

  1. Use a client-side date/time in the title.
  2. Use an extra operation to read back the server-side data, and add it to the title.
  3. Keep the date as a separate field (like you have now), and add it to the title whenever read read the document.

Upvotes: 0

Related Questions