搜索插件、代码仓与 APP 分类

插件详情v3.7.1

timeago

一个用于创建模糊时间戳的库。(例如:“15分钟前”)

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
75.8万
喜欢数
2020
Pub 分数
160

安装插件

flutter pub add timeago

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

timeago 包提供了一种简单的方法来生成人类可读的模糊时间戳,例如“15分钟前”或“2天前”。它非常适合在应用程序中显示相对时间,通过使时间更直观来提升用户体验。该库支持多种语言,并且与所有主要平台兼容,包括 Android、iOS、Web、macOS、Windows 和 Linux。它可以通过简单的 API 无缝集成到 Flutter 项目中。

适合场景

  • 在社交媒体应用中显示帖子时间戳
  • 在聊天界面中显示活动时间
  • 通过可读的时间标签提升用户体验
  • 在仪表板中跟踪事件持续时间
  • 与相对时间同步实时更新
  • 使用当前时间差动态更新UI

主要特点

  • 支持多种语言
  • 与 Flutter 的轻松集成
  • 跨平台兼容性
  • 可自定义的格式化选项
  • 自动时间更新
  • 轻量且高效

适合对象

  • 社交媒体应用
  • 实时消息应用
  • 仪表板小部件
  • 用户活动跟踪系统
  • 任何需要可读时间戳的应用程序

使用建议

  • 需要手动处理时区差异
  • 可能需要为非英文语言设置本地化
  • 不支持绝对日期格式化
  • 仅限相对时间显示
  • 不适合用于精确时间戳存储

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

timeago

timeago is a dart library that converts a date into a humanized text. Instead of showing a date 2020-12-12 18:30 with timeago you can display something like "now", "an hour ago", "~1y", etc

timeago pub package core library
timeago_flutter pub package flutter widgets

The easiest way to use this library via top-level function format(date):

import 'package:timeago/timeago.dart' as timeago;

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

    print(timeago.format(fifteenAgo)); // 15 minutes ago
    print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m
    print(timeago.format(fifteenAgo, locale: 'es')); // hace 15 minutos
}
IMPORTANT

timeago library ONLY includes en and es messages loaded by default.

To add more of the supported languages use timeago.setLocaleMessages(..). See locale messages.

Standard for language code

This library uses ISO 639-1 language code to identify the language. For more information see ISO 639-1.

Adding locales
timeago.setLocaleMessages('fr', timeago.FrMessages()); // Add french messages

print(timeago.format(fifteenAgo, locale: 'es')); // environ 15 minutes
Overriding locales or adding custom messages
// Override "en" locale messages with custom messages that are more precise and short
timeago.setLocaleMessages('en', MyCustomMessages());


// my_custom_messages.dart
class MyCustomMessages implements LookupMessages {
  @override String prefixAgo() => '';
  @override String prefixFromNow() => '';
  @override String suffixAgo() => '';
  @override String suffixFromNow() => '';
  @override String lessThanOneMinute(int seconds) => 'now';
  @override String aboutAMinute(int minutes) => '${minutes}m';
  @override String minutes(int minutes) => '${minutes}m';
  @override String aboutAnHour(int minutes) => '${minutes}m';
  @override String hours(int hours) => '${hours}h';
  @override String aDay(int hours) => '${hours}h';
  @override String days(int days) => '${days}d';
  @override String aboutAMonth(int days) => '${days}d';
  @override String months(int months) => '${months}mo';
  @override String aboutAYear(int year) => '${year}y';
  @override String years(int years) => '${years}y';
  @override String wordSeparator() => ' ';
}

Scope

While there are many request for adding more complex functionality I want keep this library as simple as possible to allow minimal maintenance.

The focus of this library should be

  1. Provide a single format function that transforms a date to a humanized value
  2. Give the abstractions for users to add their own languages or overriding them as they please
  3. Provide languages contributed by the community so users can add them as they need we should not add all languages by default.
  4. Library should not depend on any dependency

timeago_flutter widgets

  • Timeago
  • TimerRefresh
  • TimerRefreshWidget

Local development

  1. Install Melos (https://pub.dev/packages/melos):

dart pub global activate melos

  1. Bootstrap dependencies:

melos bootstrap

  1. Open desired package in VSCode or Webstorm

Live Demo

Here

所属分类

依赖

依赖清单 1 项

  • intl>=0.18.0 <0.21.0

源码仓库

使用此插件的项目

所在榜单