如何在 Flutter 中读取 Firestore 时间戳 [英] How to read Firestore timestamp in Flutter
本文介绍了如何在 Flutter 中读取 Firestore 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 Firestore 时间戳更改为2 天前或 1 小时前"之类的内容?我尝试直接显示它,但输出的数据是一个类似 Timestamp(seconds=1556459022, nanosecond=0)
的字符串.
How to change the firestore timestamp to something like "2 days ago or 1 hour ago" ? I tried displaying it directly but the data that came out was a string like Timestamp(seconds=1556459022, nanosecond=0)
.
怎么做?
推荐答案
Firestore 的时间戳有一个 toDate()
方法将返回一个飞镖 DateTime
对象.
Firestore's timestamp has a toDate()
method that will return a dart DateTime
object.
从中,您可以使用常规 dart 解决方案,例如 DateFormat
或 timeago
库显示如下:
From that you can use regular dart solutions, like DateFormat
or the timeago
library to display it as in:
timeago.format(firestoreTimestamp.toDate());
这篇关于如何在 Flutter 中读取 Firestore 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文