搜尋套件、儲存庫與 APP 分類

套件詳情v3.0.1

get_time_ago

一個 Dart 套件,用於將 `DateTime` 物件轉換並格式化為人類可讀的「多久以前」字串,例如「20 秒前」、「1 分鐘前」或「7 小時前」。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1.1萬
喜歡數
168
Pub 分數
160

安裝套件

flutter pub add get_time_ago

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個輕量級的 Dart 套件,可將 DateTime 物件轉換為人類可讀的「多久以前」字串,例如「20 秒前」或「7 小時前」。它簡化了應用程式中相對時間戳的顯示,透過以自然、對話式格式呈現時間來提升使用者體驗。此套件支援多個平台,並容易與 Flutter 專案整合。

適用情境

  • 在社交媒體應用中顯示貼文時間戳
  • 在聊天介面中顯示訊息發送時間
  • 使用相對時間標籤更新活動動態
  • 透過自然語言時間提升使用者介面可讀性
  • 即時應用程式中的事件持續時間追蹤

主要特色

  • 簡單的 DateTime 轉字串轉換
  • 支援多種語言(透過地區化)
  • 跨平台相容性
  • 所需的樣板程式碼最少
  • 可設定的精確度與輸出格式

適合對象

  • Flutter 開發人員建構即時應用
  • 需要直覺時間顯示的應用程式
  • 專注於使用者體驗的專案,包含帶時間戳的內容
  • 尋求最小化日期格式化解決方案的開發者

使用建議

  • 不支援自定區域設定格式化
  • 僅限相對時間格式,不包含絕對日期
  • 需要手動處理時區差異

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

get_time_ago

A Dart package that converts and formats DateTime objects into human-readable 'time ago' strings, such as '20 seconds ago', 'a minute ago', or '7 hours ago'. This package provides an easy way to display relative time differences in your Flutter or Dart applications. It supports various time units (seconds, minutes, hours, days, etc.) and automatically adjusts the format based on how recent the event occurred. Ideal for displaying timestamps in news feeds, social media posts, or chat messages.

pub package [Stars][repo] [Forks][repo] [Watchers][repo] [Contributors][repo]

[GitHub code size in bytes][repo] [GitHub repo size][repo] [GitHub language count][repo] [GitHub top language][repo] [GitHub Sponsors][repo]

[GitHub release (latest by date)][releases] [GitHub release (latest by date)][releases] [GitHub last commit][repo] [GitHub issues][issues] [GitHub pull requests][pulls] [GitHub License][license]

Table of Contents

Breaking Changes for the Version ^2.0.0

1. Added justNow Method in Messages Interface

In version 2.0.0, a new method justNow(int seconds) has been added to the Messages interface. This method is responsible for providing a custom message when the elapsed time is less than 15 seconds.

Impact on Custom Implementations:

If you have implemented any custom locales by extending the Messages interface, you will need to update your implementation to include the new justNow method. Failing to implement this method will result in compilation errors.

Example of Custom Locale Update:

Before:

class MyCustomMessages implements Messages {
  @override
  String prefixAgo() => '';

  @override
  String suffixAgo() => 'ago';

  // Implement the other methods here...
}

After (Version 2.0.0):

class MyCustomMessages implements Messages {
  @override
  String prefixAgo() => '';

  @override
  String suffixAgo() => 'ago';

  @override
  String justNow(int seconds) => 'just now';  // New method

  // Implement the other methods here...
}

This section explains the breaking change, the impact it has on custom implementations, and provides an example of how to update existing code to comply with the new changes in version ^2.0.0.

Demo

Click here to experience the demo in a Web App

Installation

Add get_time_ago as a dependency in your pubspec.yaml file.

dependencies:
  get_time_ago: ^latest_version

Usage

Format any DateTime object into get_time_ago format by following steps:

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

// Pass DateTime object as argument in the method
var _dateTime = DateTime.now().subtract(const Duration(minutes: 10)); // [DateTime] object
print(GetTimeAgo.parse(_dateTime)); // 10 minutes ago

// Formatting with locale
print(GetTimeAgo.parse(_dateTime, locale:'es')); // hace 10 minutos
Formatting String as get_time_ago

If you have saved a DateTime object as a String into a variable, database or cloud, then you have to first convert the String into DateTime object and then pass it as argument in parse method of get_time_ago plugin to format it into get_time_ago format by following steps:

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

var _timestamp = '2021-05-10 05:21:37.712498'; // [DateTime] formatted as String.
var _convertedTimestamp = DateTime.parse(_timestamp); // Converting into [DateTime] object
var result = GetTimeAgo.parse(_convertedTimestamp);
print(result);
Setting default locale

If you want to change your default locale, then call setDefaultLocale method and pass the locale code as the argument.

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

@override
void initState() {
  super.initState();
  GetTimeAgo.setDefaultLocale('fr'); // Sets the default locale to French
}
Setting Custom Locale & Messages

Implementing and Adding Custom Messages

class CustomMessages implements Messages {
  /// Prefix added before the time message.
  @override
  String prefixAgo() => '';

  /// Suffix added after the time message.
  @override
  String suffixAgo() => 'ago';

  /// Message when the elapsed time is less than 15 seconds.
  @override
  String justNow(int seconds) => 'just now';

  /// Message for when the elapsed time is less than a minute.
  @override
  String secsAgo(int seconds) => '$seconds seconds';

  /// Message for when the elapsed time is about a minute.
  @override
  String minAgo(int minutes) => 'a minute';

  /// Message for when the elapsed time is in minutes.
  @override
  String minsAgo(int minutes) => '$minutes minutes';

  /// Message for when the elapsed time is about an hour.
  @override
  String hourAgo(int minutes) => 'an hour';

  /// Message for when the elapsed time is in hours.
  @override
  String hoursAgo(int hours) => '$hours hours';

  /// Message for when the elapsed time is about a day.
  @override
  String dayAgo(int hours) => 'a day';

  /// Message for when the elapsed time is in days.
  @override
  String daysAgo(int days) => '$days days';

  /// Word separator to be used when joining the parts of the message.
  @override
  String wordSeparator() => ' ';
}

Overriding en Locale Messages with Custom Messages

GetTimeAgo.setCustomLocaleMessages('en', CustomMessages());

Supported Languages

  • Arabic
  • English
  • Spanish
  • Persian (Farsi)
  • French
  • Hindi
  • Portuguese (Brazil)
  • Portuguese (Brazil alternate)
  • Simplified Chinese
  • Traditional Chinese
  • Japanese
  • Occitan
  • Korean
  • German
  • Indonesian
  • Turkish
  • Urdu
  • Vietnamese
  • Romanian
  • Dutch
  • Nepali
  • Italian
  • Open to accept PR for adding more languages

Contributing

If you would like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request. Please follow the guidelines in the CONTRIBUTING.md file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributors

Made with contrib.rocks.

Support My Work

Your support helps me dedicate more time to developing high-quality, impactful projects in the open-source community. Sponsor me, and together, let’s bring even more innovation to life!

Sponsor

ko-fi

"Buy Me A Coffee"

Connect With Me

GitHub: nixrajput Linkedin: nixrajput Instagram: nixrajput Twitter: nixrajput07 Telegram: nixrajput [Gmail: nkr.nikhi.nkr@gmail.com][gmail]

Activities

Alt

[gmail]: mailto:nkr.nikhil.nkr@gmail.com [releases]: https://github.com/nixrajput/get_time_ago/releases [repo]: https://github.com/nixrajput/get_time_ago [issues]: https://github.com/nixrajput/get_time_ago/issues [license]: https://github.com/nixrajput/get_time_ago/blob/master/LICENSE [pulls]: https://github.com/nixrajput/get_time_ago/pulls

所屬分類

依賴

依賴清單 1 項

  • intl>=0.19.0 <0.21.0

原始碼儲存庫

使用此套件的專案