0

I want to get the date from the Firestore server for consistency, and not from the device. Timestamp(Date()).toDate() gives time as July 6, 2021 at 9:23:34 PM UTC+0 (timestamp). But I need only the date part, such as '11-11-2021'. Is there any way to save only the date part with Firebase server timestamp?

Code:

val docData = hashMapOf(
            "stringExample" to "Hello world!",
            "dateExample" to Timestamp(Date()).toDate(),  //want only date part. Also is there alternative of using Date()?
            "nullExample" to null
        )

db.collection("data").document("one")
    .set(docData)
    .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") }
    .addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) }

Want to do this because it's better not to strip data on some usage such as for small iot device.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Rifat
  • 1,700
  • 3
  • 20
  • 51

2 Answers2

1

There's no method on the firebase.firestore.Timestamp class that will achieve what you're looking for. You could format the dates once they've been retrieved however, and if you're looking for a quick date formatting for display then perhaps .toDateString() could be useful. Otherwise you could create your own property on the firestore documents and manually assign just the date when adding docs.

Porter
  • 193
  • 1
  • 8
1

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:

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().

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you for great explanation. What if I use the string value of time/date as collection name? And to order the result, Do I have to do it on device? If not please share any code example. – Rifat Jul 06 '21 at 16:30
  • You cannot order collections. If you wanted to say, use string value of time/date as document name, no, because you'll end up having the same issue, because document IDs are **always** Strings. – Alex Mamo Jul 06 '21 at 16:48
  • For a field value, regardless of space requiment, can I store string value of date such as 11-11-2021 or 11:11:21 or any simple format, with `server Timestamp`, without further processing on device that puts value and for removing requriement of processing on device that will use it? – Rifat Jul 06 '21 at 16:52
  • 1
    No. The value of `FieldValue.serverTimestamp()` is actually a **token** that gets sent to Firebase Firestore servers, where the actual Timestamp is determined and written into the database. So you cannot write a String or any computations of it. – Alex Mamo Jul 06 '21 at 17:07