You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何优雅计算相对时间差?需输出如‘2h 28mins ago’格式

更简洁的相对时间差实现方案

你可以通过结构化规则列表替代嵌套if-else,让逻辑更清晰、易维护。核心思路是先计算时间差的关键维度(年、月、日、时、分、秒),再按优先级从高到低匹配规则,直接返回对应格式的字符串。

以下是针对你需求的Dart实现代码:

String getRelativeTime(DateTime date) {
  final now = DateTime.now();
  final diff = now.difference(date);
  
  // 计算跨年月的差值(Duration无法准确处理跨月/跨年的月数差)
  final yearDiff = now.year - date.year;
  final monthDiff = now.month - date.month + yearDiff * 12;

  // 按优先级从高到低定义规则:[判断条件, 结果生成函数]
  final timeRules = [
    // 1年及以上
    (
      condition: yearDiff >= 1 && monthDiff >= 12,
      result: () => "a year ago"
    ),
    // 2个月及以上
    (
      condition: monthDiff >= 2,
      result: () => "a few months ago"
    ),
    // 刚好1个月,但天数差超过13天(接近2周)
    (
      condition: monthDiff == 1,
      result: () {
        final daysInTargetMonth = DateTime(now.year, now.month, 0).day;
        final actualDayDiff = (daysInTargetMonth - date.day) + now.day;
        return actualDayDiff > 13 ? "a few weeks ago" : "a few months ago";
      }
    ),
    // 1周及以上
    (
      condition: diff.inDays >= 7,
      result: () => "a week ago"
    ),
    // 1天及以上
    (
      condition: diff.inDays >= 1,
      result: () => "a day ago"
    ),
    // 1小时及以上
    (
      condition: diff.inHours >= 1,
      result: () {
        final hours = diff.inHours;
        final minutes = diff.inMinutes % 60;
        return minutes == 0 
            ? "$hours${hours == 1 ? 'h' : 'h'} ago" 
            : "$hours${hours == 1 ? 'h' : 'h'} ${minutes}mins ago";
      }
    ),
    // 1分钟及以上
    (
      condition: diff.inMinutes >= 1,
      result: () => diff.inMinutes == 1 ? "1 min ago" : "a few mins ago"
    ),
    // 小于1分钟(兜底)
    (
      condition: true,
      result: () => "a few seconds ago"
    ),
  ];

  // 遍历规则,返回第一个匹配的结果
  for (final rule in timeRules) {
    if (rule.condition) {
      return rule.result();
    }
  }

  // 理论上不会执行到这里,兜底返回
  return "a few seconds ago";
}

方案优势

  1. 逻辑清晰:所有规则集中管理,避免多层嵌套的if-else,可读性大幅提升。
  2. 易扩展:如果需要新增格式(比如"2 days ago"),只需在规则列表中添加新项即可。
  3. 准确性:单独处理跨年月的差值,避免Duration在跨月场景下的天数计算误差。
  4. 贴合需求:完全匹配你指定的输出格式,包括2h 28mins ago、a few seconds ago等所有场景。

内容的提问来源于stack exchange,提问作者Shehan DMG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 00:20:59