Convert date to timestamp for storing into firebase firestore in javascript
Solution 1
You can simply use the following line:
var myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
.fromDate
is a static method from the static Timestamp class from Firebase.
Update:
For cloud functions look at JGuo's comment:
If you are writing cloud functions, it becomes
admin.firestore.Timestamp.fromDate()
– JGuo Jan 21 at 1:56
Solution 2
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)
where x
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.
Solution 3
I solved it by using:
const created = firebase.firestore.Timestamp.fromDate(new Date()).toDate();
You have to import firebase from 'firebase/app'
explicitly in your code.
import * as firebase from 'firebase/app'
Make sure your created
or whatever constant is in type Date
before actually being saved to firestore.
Solution 4
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
allow create: if request.resource.data.date_added == request.time &&
// other rules for the message body
Client side JS code
const message = {
date_added: firebase.firestore.FieldValue.serverTimestamp();
}
Arjen de Jong
Updated on October 24, 2021Comments
-
Arjen de Jong over 2 years
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?
-
Jake Lee over 5 yearsNot sure this would fit the OP's followup comments: "I cannot use the firebase.database.ServerValue.TIMESTAMP value because some of the dates I'm using aren't the current dates."
-
Jack Woodward over 5 yearsFair enough. This is more of a create rule and in that instance all the dates are new so you could use this. Then using an allow update rule that says you cant change its value as thats not when it was added. Then it would just be a case of updating historic data if it exists.
-
Arjen de Jong over 5 yearsI agree that my wording for the date variable should be something else instead of 'date_added'. However I'm grateful for your answer as I might use it someday!
-
JCisar about 5 yearsThank you this is the simplest solution and exactly what I was looking for! For some reason the documentation I found said you passed the date in via the constructor not via a static method.
-
1020rpz over 4 yearsSending new Date() or Date.now() directly is totally enough. Firebase/Firestore handle the convertion to a timestamp on his own. Plus you can use moment.js to handle all your date manipulation
-
Jack Guo over 4 yearsif you are writing cloud functions, it becomes
admin.firestore.Timestamp.fromDate()
-
Nehal almost 4 yearsHow to access this function in App Script?
-
Constantine almost 3 yearsHi, Doug) Can You provide example how to create Timestamp on the Angular application?
-
danday74 almost 3 yearsagreed that sending new Date() is enough, but Typescript wants a type - either Date or firebase.firestore.Timestamp (could use a union type but don't like to)
-
Beanwah over 2 yearsI had to use firebase.default.firestore
-
Beanwah over 2 yearsIf your object has logic in its constructor you can do a JSON.parse(JSON.stringify(myObject)) to remove that from the object prototype before sending to Firestore. This ultimately made the custom object error go way for me.