Raghava Reddy
Raghava Reddy

Reputation: 13

How to show date from firestore (timestamp) in flutter?

I am getting timestamp from firestore. How to show it in Date/month/year in flutter? Tried so many ways but it shows error: String is subtype of Timestamp or datetime. Below is my code.

   itemCount: orderList.data.docs.length,
          itemBuilder: (ctx, index) {
            final cartOrder = orderList.data.docs[index]['cartorder'];
            final Timestamp orderedDate =
                orderList.data.docs[index]['ordered_date'];
            final delivered = orderList.data.docs[index]['delivered'];
            String myDate =
                DateFormat('dd/MM/yyyy,hh:mm').format(orderedDate.toDate());

Thanks a lot.

Upvotes: 0

Views: 1593

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17822

You are getting a timestamp. Use DateTime.fromMillisecondsSinceEpoch

DateTime date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
String myDate = DateFormat('dd/MM/yyyy,hh:mm').format(date);

Upvotes: 3

Related Questions