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

套件詳情v1.1.1

simple_moment

Moment.js 相對時間功能的簡單實作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
483
喜歡數
51
Pub 分數
120

安裝套件

flutter pub add simple_moment

套件介紹

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

一個輕量級的 Flutter 套件,帶來類似 Moment.js 的基本功能,用於顯示相對時間。它簡化了時間差的格式化,例如「2 分鐘前」或「5 小時後」,適用於多個平台。設計簡單易用,提供清晰的 API,可將時間戳轉換為人類可讀的相對字串,且無繁重依賴。非常適合需要在使用者介面中直覺呈現時間的應用。

適用情境

  • 顯示貼文時間戳
  • 顯示訊息發送時間
  • 更新即時活動指標
  • 增強使用者通知
  • 追蹤事件截止日期

主要特色

  • 簡單的相對時間格式化
  • 跨平台支援
  • 最小依賴足跡
  • 與 DateTime 物件的輕鬆整合
  • 不需要外部函式庫

適合對象

  • 具備即時更新的應用程式
  • 社群媒體介面
  • 聊天應用
  • 任務管理工具
  • 任何需要可讀時間差的應用程式

使用建議

  • 僅限相對時間
  • 無日期解析或操作
  • 不支援本地化
  • 不適用於複雜的日期操作
  • 需要手動處理時區偏移

原始 README

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

simple_moment Pub Build Status

A simple implementation of the Moment.js features for relative time in Dart.

Usage

A simple usage example:

import 'package:simple_moment/simple_moment.dart';

main() {
    var secondsToAdd = Duration(seconds: 10);
    var dateForComparison = DateTime.now().add(secondsToAdd);
    var moment = Moment.now();

    // should print "in a few seconds"
    print(moment.from(dateForComparison));
    
    Moment rawDate = Moment.parse("2020-07-03");
      // should print "2020-08-03 00:00:00.000
      print(rawDate.add(months: 1));
    
      // should print "2019-07-03 00:00:00.000
      print(rawDate.subtract(years: 1));
    
      //should print "03-07-2020 00:00"
      print(rawDate.format("dd-MM-yyyy HH:mm"));
}

Locales

Set the locale for all usages of Moment:
Moment.setLocaleGlobally(new LocaleDe());
Set the locale only for the current instance of Moment:
var moment = new Moment.now().locale(new LocaleDe());
Adding your own locale:

Just create a class that implements ILocaleData and assign that to your Moment instance or set it globally.

Overwriting existing locales:
class ShortLocaleEn extends LocaleEn {
  String get seconds => '%is';

  String get aMinute => '%im';
  String get minutes => '%im';

  String get anHour => '%ih';
  String get hours => '%ih';

  String get aDay => '%id';
  String get days => '%id';
}
Using intl to format dates
// create new Moment instance with german locale
var moment = Moment.now().locale(LocaleDe(), useInFormat: true);

// initialize intl
await initializeDateFormatting(moment.usedLocale.localeString);

// format and print the current month in german
var formattedString = moment.format("LLLL");
print(formattedString);

Features and bugs

Please file feature requests and bugs at the issue tracker.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案