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:
- you attach a listener
- you write a value with
ServerValue.TIMESTAMP
- the Firebase client immediate fires a value event with an approximation of the timestamp it will write on the server
- your code prints that value
- the write operation gets sent to the Firebase servers
- the Firebase servers determine the actual timestamp and write the value to the database (assuming no security rules fail)
- the Firebase server send the actual timestamp back to the client
- the Firebase client raises a value event for the actual value
- 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
Android Eclipse Ddms - Can't Access Data/Data/ on Phone to Pull Files
Add New Item Count to Icon on Button - Android
Get Center Visible Item of Recycleview When Scrolling
Viewpager with Google Maps API V2: Mysterious Black View
Blur or Dim Background When Android Popupwindow Active
Use Tab with New Toolbar (Appcompat V7-21)
Android: How to Overlay a Bitmap and Draw Over a Bitmap
Android; Check If File Exists Without Creating a New One
Disable Activity Slide-In Animation When Launching New Activity
Android - Drawable with Rounded Corners at the Top Only
Making Data Persistent in Android
Android: Using Linear Gradient as Background Looks Banded
Android: Scrolling an Imageview
Fragment Onresume() & Onpause() Is Not Called on Backstack
Install_Failed_Duplicate_Permission... C2D_Message
Android Webview Style Background-Color:Transparent Ignored on Android 2.2