0

I need to display date from firebase and not timestamp: screenshot of current behavior

fun getTimeDate() {
    prayerreference = FirebaseDatabase.getInstance().getReference("PrayerInfo").child("time")
    prayerreference!!.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (ds in dataSnapshot.children) {
                    val timestamp = ds.child("PrayerInfo").child("time").getValue(Long::class.java)

                    // timepost.text= DateDifference(0)
                    //val time = prayerreference.toString()
                    timepost.text = timestamp.toString()
                                //System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)
                    //            DateUtils.MINUTE_IN_MILLIS, flags).toString(
                    //Log.d(ContentValues.TAG, getTimeDate(date).toString())
                }

        }
        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(ContentValues.TAG, databaseError.message) //Don't ignore errors!
        }
    })
    //prayerreference!!.child("time").addListenerForSingleValueEvent()
}

It only displays timestamp from firebase but need it to be date on every post.

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734

1 Answers1

0

To convert a timestamp (in milliseconds since the epoch) to a Date, you can pass its value to the constructor of Date. So:

timepost.text = new Date(timestamp.toString())

Also see:

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • How do I capture that timestamp from firebase..? It only works when the timestamp is hardcoded in the project. – Evansito Robbie Jun 03 '22 at 12:19
  • The code you shared to do so looks fine at first glance, so more likely the code doesn't match the data. If you also want our help with that, edit your question to show the data that is being read (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 03 '22 at 13:13