How to Use the Firebase Server Timestamp to Generate Date Created

How to use the Firebase server timestamp to generate date created?

When you use the ServerValue.TIMESTAMP constant in a write operation, you're saying that the Firebase Database server should determine the correct timestamp when it executes the write operation.

Let's say we run this code:

ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot.getValue());
}

public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);

This will execute as follows:

  1. you attach a listener
  2. you write a value with ServerValue.TIMESTAMP
  3. the Firebase client immediate fires a value event with an approximation of the timestamp it will write on the server
  4. your code prints that value
  5. the write operation gets sent to the Firebase servers
  6. the Firebase servers determine the actual timestamp and write the value to the database (assuming no security rules fail)
  7. the Firebase server send the actual timestamp back to the client
  8. the Firebase client raises a value event for the actual value
  9. your code prints that value

If you're using ChildEventListener instead of a ValueEventListener, then the client will call onChildAdded in step 3 and onChildChanged in step 8.

Nothing changed in the way we generate the ServerValue.TIMESTAMP since Firebase joined Google. Code that worked before, will continue to work. That also means that the first answer you linked is a valid way to handle it.

get simple date on firebase server timestamp? How to get only date not time

The most appropriate way for saving the Date and Time would be to set a Timestamp field, as explained in my answer from the following code:

  • How to add a Timestamp in Firestore with Android?

If you try to save the dates as String values '11-11-2021', then you'll not be able to order the results, because when you order String elements, the order is lexicographical. Besides that, in terms of storage, the Date field will occupy less space than '11-11-2021'. According to the official documentation regarding storage field size calculation:

The size of Date field values is 8 bytes, while the value of String text is the number of UTF-8 encoded bytes + 1.

So we have 8 bytes vs. 11 bytes. So the best option that you have, is to store the Date as a Firestore Timestamp using FieldValue.serverTimestamp().

How to convert selected timestamp into firebase server timestamp android

You didn't specify whether you're using Java or Kotlin, however you can create a Firestore Timestamp from an Android date

When calling the Timestamp constructor you would pass in your date object,so make sure you're using valid Dates as outlined in the Android documentation.

Add timestamp in Firestore documents

firebase.firestore.FieldValue.serverTimestamp()

Whatever you want to call it is fine afaik. Then you can use orderByChild('created').

I also mostly use firebase.database.ServerValue.TIMESTAMP when setting time

ref.child(key).set({
id: itemId,
content: itemContent,
user: uid,
created: firebase.database.ServerValue.TIMESTAMP
})

How do I convert a Firestore date/Timestamp to a JS Date()?

The constructor for a JavaScript's Date doesn't know anything about Firestore's Timestamp objects — it doesn't know what to do with them.

If you want to convert a Timestamp to a Date, use the toDate() method on the Timestamp.

How to get server Timestamp from firebase v9?

It's discussed in the documentation. Just import serverTimestamp.

import { updateDoc, serverTimestamp } from "firebase/firestore";

const docRef = doc(db, 'objects', 'some-id');

// Update the timestamp field with the value from the server
const updateTimestamp = await updateDoc(docRef, {
timestamp: serverTimestamp()
});

how to store date in the firebase with type 'timestamp' instead of 'map' from reactjs

I think you will need to do something like this to save it as timestamp on firebase.

firebase.firestore.Timestamp.fromDate(date).toDate());


Related Topics



Leave a reply



Submit