简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM