【问题标题】:How to show Timestamp String in DateFormat in flutter firebase [duplicate]如何在flutter firebase中以DateFormat显示时间戳字符串[重复]
【发布时间】:2021-09-08 05:48:25
【问题描述】:
  • 我正在使用document['eventDate'] 直接获取数据库中的时间戳存储我无法显示时间戳所以我将其转换为字符串document['eventDate'].toString(),它给了我你可以在下面看到的输出
  • 我尝试将其转换为日期时间格式DateFormat('dd-MM-yyyy'),但显示错误提示 Class 'DateTime' has no instance method 'DateFormat'。 有什么方法可以显示日期。
  • 数据以以下格式存储

  • 代码
  Widget getHomePageBody(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection('events').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(
              child: CircularProgressIndicator(),
            );
          default:
            return new ListView(
              padding: EdgeInsets.only(bottom: 80),
              children: snapshot.data.docs.map((DocumentSnapshot document) {
                return Padding(
                  padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
                  child: Card(
                    child: ListTile(
                      leading: new Image.network(
                        document['eventImageUrl'],
                        fit: BoxFit.cover,
                        width: 120.0,
                      ),
                      title: new Text(
                        'Date:' + (document['eventDate'].toDate()).DateFormat('kk:mm:a').format(),
                        style: new TextStyle(
                            color: Color(0xFF49DEE8),
                            fontSize: 14.0,
                            fontWeight: FontWeight.normal),
                      ),
                    ),
                  ),
                );
              }).toList(),
            );
        }
      },
    );
  }

【问题讨论】:

标签: firebase flutter google-cloud-firestore


【解决方案1】:
                            Expanded(
                              child: Center(
                                child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text(
                                    model.hwPublishedDate
                                            .toDate()
                                            .hour
                                            .toString() +
                                        ":" +
                                        model.hwPublishedDate
                                            .toDate()
                                            .minute
                                            .toString() +
                                        " " +
                                        model.hwPublishedDate
                                            .toDate()
                                            .timeZoneName
                                            .toString() +
                                        ", " +
                                        model.hwDate,
                                    style: GoogleFonts.abel(
                                        color: Colors.green[500],
                                        fontSize: 15.0,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
               ListTile(
                      leading: Icon(
                        Icons.calendar_today_sharp,
                        color: Colors.green[900],
                      ),
                      title: DateTimeField(
                        validator: (value) =>
                            value == null ? 'Provide Upload Date' : null,
                        showCursor: true,
                        cursorColor: Colors.green,
                        textCapitalization: TextCapitalization.words,
                        style: TextStyle(color: Colors.green[900]),
                        controller: _hwdateTextEditingController,
                        decoration: InputDecoration(
                            hintText: "Date",
                            hintStyle: TextStyle(
                              color: Colors.green[900],
                            ),
                            border: InputBorder.none),
                        format: DateFormat("dd-MM-yyyy"),
                        onShowPicker: (context, currentValue) {
                          return showDatePicker(
                              context: context,
                              firstDate: DateTime(1947),
                              initialDate: currentValue ?? DateTime.now(),
                              lastDate: DateTime(2100));
                        },
                        onChanged: (selectedCateg) {
                          setState(() {
                            queryString = selectedCateg.toString();
                            classSec = selectedClass + selectedSec;
                          });
                          startSearching(classSec);
                        },
                      ),
                    ),

您首先使用 .toDate().toString() 然后使用日期格式对其进行格式化,Listile 是我存储日期的方式,如果您愿意,您也可以这样使用它

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2021-07-15
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多