【问题标题】:Why is the Flutter timeago package not displaying correctly, or is there something wrong with my code?为什么 Flutter timeago 包显示不正确,还是我的代码有问题?
【发布时间】:2020-03-07 01:48:57
【问题描述】:

这是我获取 DateTime 值以查看示例输出的地方。

@override
  Widget build(BuildContext context) {

    print('Datetime UTC: ${DateTime.now().toUtc()}');
    print('Date from Json: $date');
    print('Time ago from Json: ${timeago.format(date)}');
    print('Time ago from Json using UTC: ${timeago.format(date, clock: DateTime.now().toUtc())}');


    return GestureDetector(...

这成为输出显示。我现在的时间是 2020-03-07 09:42 am。 时间“来自 Json 的时间:”是正确的,但“来自 Json 的时间之前使用 UTC:”是不正确的。

I/flutter (27117): Datetime UTC: 2020-03-07 01:42:59.742488Z
I/flutter (27117): Date from Json: 2020-03-07 00:32:31.000
I/flutter (27117): Time ago from Json: 9 hours ago
I/flutter (27117): Time ago from Json using UTC: 9 hours ago

相同的输出,使用 timeago 包

【问题讨论】:

  • 我知道可能是原因,如果它的输出变得相同,系统时间就是原因

标签: datetime flutter dart timeago flutter-packages


【解决方案1】:
var currentTime = DateTime.now();
var jsonTime = $date


print(timeago.format(currentTime.subtract(jsonTime));

如果您查看timeago.format() 的参数,您会发现它采用Duration 作为参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多