Convert date to timestamp for storing into firebase firestore in javascript

59,948

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.

Ref: Firebase Timestamp Doc

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();
}
Share:
59,948
Arjen de Jong
Author by

Arjen de Jong

Updated on October 24, 2021

Comments

  • Arjen de Jong
    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.

    number

    I would like to have it inserted as shown below (as a timestamp, not as a number).

    enter image description here

    Is this possible?

  • Jake Lee
    Jake Lee over 5 years
    Not 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
    Jack Woodward over 5 years
    Fair 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
    Arjen de Jong over 5 years
    I 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
    JCisar about 5 years
    Thank 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
    1020rpz over 4 years
    Sending 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
    Jack Guo over 4 years
    if you are writing cloud functions, it becomes admin.firestore.Timestamp.fromDate()
  • Nehal
    Nehal almost 4 years
    How to access this function in App Script?
  • Constantine
    Constantine almost 3 years
    Hi, Doug) Can You provide example how to create Timestamp on the Angular application?
  • danday74
    danday74 almost 3 years
    agreed 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
    Beanwah over 2 years
    I had to use firebase.default.firestore
  • Beanwah
    Beanwah over 2 years
    If 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.