【问题标题】:How to convert firebase timestamp to datetime in flutter如何在颤动中将firebase时间戳转换为日期时间
【发布时间】:2022-06-11 00:15:44
【问题描述】:

我尝试使用流从 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);
                      }
                    })

【问题讨论】:

标签: flutter firebase google-cloud-firestore flutter-streambuilder


【解决方案1】:
DateTime.fromMicrosecondsSinceEpoch(data['paidDate']).toString();

对于dd-mm-yyyy 格式使用国际包

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2021-08-26
    • 2019-04-03
    • 1970-01-01
    • 2020-03-30
    • 2022-09-30
    • 2018-12-09
    • 2017-04-25
    相关资源
    最近更新 更多