I'm currently using Math.floor(Date.now() / 1000)
to get the correct timestamp format to add into a document in Firebase, however, the timestamp gets inserted as a number, and not as a timestamp.
I would like to have it inserted as shown below (as a timestamp, not as a number).
Is this possible?
You can simply use the following line:
.fromDate
is a static method from the static Timestamp class from Firebase.Ref: Firebase Timestamp Doc
As i can see you are doing date added you would be better using the Server Timestamp and using the below security rule to enforce it.
Security Rules
Client side JS code
Use
for more options of the date here the full details
If you want to store a field as a timestamp in Firestore, you'll have to send a JavaScript Date object or a Firestore Timestamp object as the value of the field.
If you want to use Date, simply say
new Date(x)
wherex
is the value in milliseconds that you were trying to add before.If you want to use Timestamp, you would have to do more work to get that
x
converted into a combination of seconds and nanoseconds to pass to the constructor, as the documentation suggests.To store a date / timestamp in Firestore, you need to send a
Date
object.For example, with an existing date, set a field to:
new Date("December 10, 1815")
Further information is available in the docs.