Home > Blockchain >  How to display only date from firestore Flutter
How to display only date from firestore Flutter

Time:05-30

I have data stored in Firestore with the field traveldate and I'm trying to print the data in Text(document['traveldate'].toString()) but in text, it shows like in UI Timestamp(seconds=xxxx, nanoseconds=0).

Card(child: ListTile(title: Text(document['Startdate'].toString(),
style: GoogleFonts.quicksand(fontStyle: FontStyle.normal)))),

CodePudding user response:

You can convert a Firestore Timestamp to a Date like this:

DateTime date = document['Startdate'].toDate();

And if you want to display it with a specific text format you can convert your date like this:

String dateFormatted = DateFormat('dd MMM, yyyy').format(date);

Based on your pasted code you can do this:

Card(child: ListTile(title: Text(document['Startdate'].toDate(),
style: GoogleFonts.quicksand(fontStyle: FontStyle.normal)))),

CodePudding user response:

You can try

Card(
child: ListTile(title: Text((document['Startdate'] as Timestamp).toDate(),
style: GoogleFonts.quicksand(fontStyle: FontStyle.normal)))
),

Don't forget to import

import 'package:cloud_firestore/cloud_firestore.dart';
  • Related