如何在flutter中将firebase时间戳转换为datetime

4si2a6ki  于 2022-11-25  发布在  Flutter
关注(0)|答案(2)|浏览(153)

我尝试使用流从firebase获取timestamp数据,并将其显示在数据表中,但时间戳的格式是(seconds=1560523991, nanoseconds=286000000)。如何将其转换为dd-mm-yyyy。我尝试使用DateFormat()解析它,但它不起作用。
代码

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);
                      }
                    })
tv6aics1

tv6aics11#

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

对于dd-mm-yyyy,使用intl包

qltillow

qltillow2#

假设数据[“paidDate”]给出:

{_seconds: 1560523991, _nanoseconds: 286000000}

并希望将其转换为DateTime,请执行以下操作(步骤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)将totalSecsToMicroSecs转换为日期时间:

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

这将为您提供一个日期时间:-)

相关问题