-3

I tried fetching timestamp data from firebase using stream and displaying it in a data table but the timestamp is the format (seconds=1560523991, nanoseconds=286000000) how to do I convert it to dd-mm-yyyy format. I tried parsing it using DateFormat() but it didn't work.

code

StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection('lender')
                        .doc(auth.currentUser!.email)
                        .collection('paymentData')
                        .where('name',
                            isEqualTo: Provider.of<UpdateNameProvider>(context,
                                    listen: false)
                                .bname)
                        //.orderBy('paidDate', descending: true)
                        .snapshots(),
                    //.snapshots(),
                    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData) {
                        return const Center(
                            child: CircularProgressIndicator(
                          backgroundColor: Color(0xff8eacbb),
                        ));
                      } else if (snapshot.data!.docs.isEmpty) {
                        return const Center(
                          child: Text(
                            'Press  +  to add data',
                            style: TextStyle(fontSize: 20),
                          ),
                        );
                      } else {
                        return Center(
                            child: Container(
                          child: DataTable(
                              columns: const [
                                DataColumn(label: Text('Amount')),
                                DataColumn(label: Text('Paid Date'))
                              ],
                              rows: snapshot.data!.docs.map((data) {
                                // DateTime datee = data['paidDate'];
                                return DataRow(cells: [
                                  DataCell(Text(data['amount'])),
                                  DataCell(Text(data['paidDate'].toString()))
                                ]);
                              }).toList()),
                        ));
                        //print(snapshot.data!.docs);
                      }
                    })
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ganesh Sivakumar
  • 149
  • 1
  • 16

3 Answers3

2
DateTime.fromMicrosecondsSinceEpoch(data['paidDate']).toString();

and for dd-mm-yyyy format use intl package

bayhas
  • 27
  • 3
  • To further expand on this, assuming you want to convert for example {_seconds: 10000, _nanoseconds: 100000} to DateTime, do this: – Manbus Nov 19 '22 at 00:04
0

Assuming data["paidDate"] gives:

{_seconds: 1560523991, _nanoseconds: 286000000} 

and you want to convert it DateTime, do this (Step 1):

var nanoSecsToSecs = currentProfileDoc["paidDate"]["_nanoseconds"] * 0.000000001;
// this converts nanoseconds to seconds

var totalSeconds = nanoToSecs + currentProfileDoc["paidDate"]["_seconds"];
// this adds all the seconds (nanoToSecs + _seconds) together

var totalSecsToMicroSecs = totalSeconds * 1000000;
// this converts seconds to microseconds

(Step 2) to convert totalSecsToMicroSecs to DateTime:

DateTime.fromMicrosecondsSinceEpoch(totalSecsToMicroSecs);
// this converts microseconds to DateTime format

This will give you a DateTime :-)

Manbus
  • 706
  • 4
  • 9
0

You can use the toDate() function:

data['paidDate'].toDate()

In case your your data is not recognized as a timestamp you can parse it before calling the toDate() method:

(data['paidDate'] as Timestamp).toDate()