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

套件詳情v5.3.4

moment_dart

多功能不可變 DateTime 子類。支援多種地區設定,可輕鬆將 DateTime 和 Duration 轉換為人類可讀的格式

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
6820
喜歡數
86
Pub 分數
160

安裝套件

flutter pub add moment_dart

套件介紹

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

moment_dart 是一個多功能、不可變的 DateTime 子類,專為在 Flutter 應用中輕鬆處理日期和時間而設計。它支援多種本地化,讓開發者能夠將 DateTime 和 Duration 物件轉換為針對不同語言和地區定制的人類可讀格式。此套件以不可變性為核心理念,確保狀態管理的安全性和可預測性。該套件可無縫整合至 Android、iOS、Web、macOS、Windows 和 Linux 平台,非常適合需要強大日期格式化和本地化功能的跨平台應用。

適用情境

  • 格式化多種語言的日期
  • 計算時間差並以可讀格式輸出
  • 建構本地化日曆介面
  • 處理時區感知的日期操作
  • 建立動態的「之前」時間字串
  • 簡化UI中的持續時間顯示

主要特色

  • 不可變的 DateTime 替代品
  • 內建地區化支援
  • 可讀性時間格式化
  • 跨平台相容性
  • 持續時間轉字串
  • 可鏈式呼叫且流暢的 API

適合對象

  • 國際化 Flutter 應用
  • 需要時間的顯示應用
  • 日曆和排程工具
  • 本地化密集型專案
  • 在 Dart 中尋找類似 moment.js 的功能的開發者

使用建議

  • 需要仔細處理時區
  • 可能會稍微增加套件大小
  • 僅限日期/時間格式化使用情境
  • 不適用於複雜的日期運算
  • 依賴外部地區化資料
  • 最佳搭配 intl 套件以獲得完整的國際化支援

原始 README

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

moment_dart

pub package License: MIT All Contributors

Inspired by moment.js

References

📖 Read documentation
🆕 Migrate to 5.x.x

Usage

Creating instance

There are multiple ways to create an instance.

// From DateTime
final a = DateTime.now().toMoment();

final b = Moment(DateTime(...));

// This instance won't be affected by global localization!
final c = Moment(b, localization: MomentLocalizations.koKr());
Setting global localization

By default, the global localization is en_US.

Moment.setGlobalLocalization(MomentLocalizations.fr());
Relative Duration

Relative durations are rather inprecise. See precise duration for accuracy and flexibility.

Read about thresholds, and more on docs

final yesterday = Moment.now() - const Duration(days: 1);
yesterday.fromNow(); // a day ago

final add5sec = Moment.now().add(const Duration(seconds: 5));
add5sec.fromNow(dropPrefixOrSuffix: true); // a few seconds
Precise duration

You can create localized human-readable duration text with abbreviation, highly-customizable units.

See more on docs. (delimiter, custom units, abbreviation...)

On Duration objects:

Duration(days: 67, hours: 3, minutes: 2).toDurationString(
  localization: MomentLocalizations.de(),
  form: Abbreviation.semi,
); // in 2 Mo. 7 Tg.

For Moment:

final epoch = Moment.fromMillisecondsSinceEpoch(0);
final now = DateTime(2023, DateTime.july, 14).toMoment();

print(epoch.fromPrecise(now)); // 53 years 7 months ago
Calendar
final Moment now = Moment.now();

print(now.calendar()); // Today at 03:00 PM
Other helpful methods

moment_dart provides numerous convenience methods. Works on both Moment and DateTime.

See all available options in the docs

final now = DateTime.now(); // July 19 2023 03:12 PM (UTC+8)

// ISO week, ISO week year
now.week; // 48
now.weekYear; // 2023

now.isLeapYear; // false

// Date creates new `DateTime` with hours, minutes, etc.. omitted
now.date; // July 19 2023 00:00 AM

now.clone(); // July 19 2023 03:12 PM

now.nextTuesday(); // July 25 2023 03:12 PM

// and many more!

Contributing

Contributions of any kind are welcome! Please refer to CONTRIBUTE.md

Contributors

Батмэнд Ганбаатар
Батмэнд Ганбаатар

💻 🚧 🌍 📖
Ultrate
Ultrate

💻 🌍
Muhammadyusuf
Muhammadyusuf

🌍 💻
Paweł
Paweł

🌍 💻

所屬分類

原始碼儲存庫

使用此套件的專案