What would you recommend to store dates using Firebase between a datetime and a timestamp, and why?
Date and time in the Firestore are saved in their proprietary date format — called Timestamp. Firestore timestamp is just a simple object with two properties — seconds and nanoseconds, which apparently has some advantages over “classic” date format.
To convert a Firestore Timestamp into a Javascript date, just call . toDate() on the Timestamp.
You can store the date as an epoch date. It's a long that you can get using your system time System. currentTimeMillis(); or by using the Firebase server time with their ServerValue. TIMESTAMP .
What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.
Use a timestamp as this is locale-agnostic and does not require formatting and parsing agreements between clients. It's also a bit shorter.
Furthermore, utilize Firebase.ServerValue.TIMESTAMP, rather than trusting the clients to have their clocks set correctly. If, for example, a client's clock is off by 5 minutes and you utilize the client timestamps in a chat conversation, then rendering back the messages, the time shown would appear to be 5 minutes in the future for other users.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With