Flutter 有没有办法制作动态时间戳,打印 10 分钟前、1 小时前或 2 个月前的内容

发布于 2025-01-16 13:16:46 字数 436 浏览 5 评论 0原文

我已经将我的lastseen时间戳发送到firebase中,如何用我已经发送到firestore的时间戳来扣除当前时间戳。这会给我Last Seen 15 分钟前,或者可能是很长一段时间,看起来像Last Seen 20 days ago 那样。我已经尝试在 pub.dev 中使用 timeago 包,但我不知道在持续时间内应该替换什么。代码看起来像这样

final fifteenAgo = DateTime.now().subtract(Duration(minutes: 15));

我应该用什么替换分钟?

这是我从 firestore 检索到的数据

I/flutter ( 6086): 2022-03-24 12:21:32.235192

i already send my lastseen timestamp into firebase and how can i deduct the current timestamp with the one that i already sent to firestore. that will give me Last Seen 15 minutes ago, or maybe a long time that look like this Last Seen 20 days ago that did it. im already try to use the timeago package in pub.dev but i dont know what should i replace in the duration. the code look like this

final fifteenAgo = DateTime.now().subtract(Duration(minutes: 15));

what should i replace the minutes with ?

this is the data that i've been retrieve from firestore

I/flutter ( 6086): 2022-03-24 12:21:32.235192

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-23 13:16:46
final DateTime lastSeen = DateTime.parse(yourDataFromFireStore);
final Duration timePassed = DateTime.now().difference(lastSeen);

timePassed 变量将保存自上次查看以来经过的持续时间。然后,您必须定义一个函数,该函数获取持续时间并返回应显示的正确字符串,例如“5 天前”或其他内容。

final DateTime lastSeen = DateTime.parse(yourDataFromFireStore);
final Duration timePassed = DateTime.now().difference(lastSeen);

The timePassed variable will then hold the duration that passed since it was last seen. You will then have to define a function that takes the duration and returns the proper string that should be displayed like for example "5 days ago" or something else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文