在Dart / Flutter中将DateTime转换为时间之前

时间:2018-11-08 07:51:58

标签: dart flutter

问题是如何将Dart DateTime格式化为字符串,说明经过的时间,类似于您在Stack Overflow上看到的时间。

有没有比这简单的方法

String timeAgo(DateTime d) {
 Duration diff = DateTime.now().difference(d);
 if (diff.inDays > 365)
  return "${(diff.inDays / 365).floor()} ${(diff.inDays / 365).floor() == 1 ? "year" : "years"} ago";
 if (diff.inDays > 30)
  return "${(diff.inDays / 30).floor()} ${(diff.inDays / 30).floor() == 1 ? "month" : "months"} ago";
 if (diff.inDays > 7)
  return "${(diff.inDays / 7).floor()} ${(diff.inDays / 7).floor() == 1 ? "week" : "weeks"} ago";
 if (diff.inDays > 0)
  return "${diff.inDays} ${diff.inDays == 1 ? "day" : "days"} ago";
 if (diff.inHours > 0)
  return "${diff.inHours} ${diff.inHours == 1 ? "hour" : "hours"} ago";
 if (diff.inMinutes > 0)
  return "${diff.inMinutes} ${diff.inMinutes == 1 ? "minute" : "minutes"} ago";
 return "just now";
}

我觉得这有点矫kill过正。谢谢,希望对其他人有帮助

3 个答案:

答案 0 :(得分:6)

我将timeago用于精确的目的,发现它非常有用。它具有多种格式,并且还支持不同的语言。

答案 1 :(得分:4)

您可以使用Flutter的默认intl.dart方法非常轻松地完成此操作

导入import 'package:intl/intl.dart';软件包并获取日期和时间之前的时间

所以让我们看看如何做到这一点,首先创建一个TimeAgo

import 'package:intl/intl.dart';

class TimeAgo{
  static String timeAgoSinceDate(String dateString, {bool numericDates = true}) {
    DateTime notificationDate = DateFormat("dd-MM-yyyy h:mma").parse(dateString);
    final date2 = DateTime.now();
    final difference = date2.difference(notificationDate);

    if (difference.inDays > 8) {
      return dateString;
    } else if ((difference.inDays / 7).floor() >= 1) {
      return (numericDates) ? '1 week ago' : 'Last week';
    } else if (difference.inDays >= 2) {
      return '${difference.inDays} days ago';
    } else if (difference.inDays >= 1) {
      return (numericDates) ? '1 day ago' : 'Yesterday';
    } else if (difference.inHours >= 2) {
      return '${difference.inHours} hours ago';
    } else if (difference.inHours >= 1) {
      return (numericDates) ? '1 hour ago' : 'An hour ago';
    } else if (difference.inMinutes >= 2) {
      return '${difference.inMinutes} minutes ago';
    } else if (difference.inMinutes >= 1) {
      return (numericDates) ? '1 minute ago' : 'A minute ago';
    } else if (difference.inSeconds >= 3) {
      return '${difference.inSeconds} seconds ago';
    } else {
      return 'Just now';
    }
  }

} 

以这种方式调用并获得所需的输出

TimeAgo.timeAgoSinceDate(item.created_date), // In this example, 09-10-2020 08:29AM date format is getting passed, but you can pass it in any format.

enter image description here

答案 2 :(得分:1)

您也可以尝试使用此软件包Jiffy

您可以从现在开始获取相对时间

// This returns time ago from now
Jiffy().fromNow(); // a few seconds ago

//You can also pass in a DateTime Object or a string or a list
Jiffy(DateTime.now()).fromNow; // a few seconds ago
//or
Jiffy(DateTime(2018, 10, 25)).fromNow(); // a year ago
Jiffy("2020-10-25").fromNow(); // in a year

在Jiffy中操作也很简单

var dateTime = Jiffy()
  ..add(hours: 3, months: 2)
  ..utc();

dateTime.fromNow(); // in 2 months

您还可以从现在开始的指定时间获取相对时间

Jiffy([2022, 10, 25]).from([2022, 1, 25]); // in 10 months